我有一個代碼:清潔RandomAccessFile的
for (int i = 1; i <= Math.ceil(n/m); i++){
RandomAccessFile file_1 = new RandomAccessFile(".\\src\\dataFiles\\fileC_" + i + ".dat", "r");
RandomAccessFile index_1 = new RandomAccessFile(".\\src\\dataFiles\\indexC_" + i + ".dat", "r");
RandomAccessFile sumFile = new RandomAccessFile(".\\src\\dataFiles\\sumFile_" + i + ".dat", "rw");
RandomAccessFile sumIndex = new RandomAccessFile(".\\src\\dataFiles\\sumIndex_" + i +".dat", "rw");
for (int q = 0; q < n; q++){
sumRow = Matrix.getString(file_1, index_1, q).plus(Matrix.getString(tempFile, tempIndex, q));
Matrix.writeLine(sumFile, sumIndex, sumRow);
}
tempFile = sumFile;
tempIndex = sumIndex;
file_1.close();
index_1.close();
}
正如你所看到的,有文件和它們的索引,這表明我在哪裏可以找到必要的字符串。 該程序的這一部分添加了位於文件中的整數。鍵入「file_1」的文件,我可以關閉並刪除,但類型「sumFile」文件不能,因爲我用referenes他們:
tempFile = sumFile;
tempIndex = sumIndex;
如果我知道如何清理文件,我不會每次需要sumFile時創建「sumFile」文件。我只是把它清理乾淨。你知道嗎,如何清理RandomAccessFile?我知道的唯一方法是這樣做:
File file = new File("path to a sumFile");
file.delete();
然後創建一個甚至名稱的新文件。
謝謝你的回答! – Dmitry 2010-03-10 22:26:29
@Dmitry你應該接受一個適合你的答案。 – 2013-02-21 17:13:33
它對我很好。 我的尾巴-f命令顯示以下消息: 'tail:/home/daniel/eclipse/results/CID-sumary.txt:截斷的文件','清理'該文件並再次在命令行上重新打印一些新的緩衝區在我的文件中。 – 2014-07-21 03:51:46