我有一个代码:清洁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