我有一个代码:
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”类型的文件不能,因为我对它们使用了引用:
tempFile = sumFile;
tempIndex = sumIndex;如果我知道如何清理文件,我就不会在每次需要sumFile时都创建"sumFile"-files。我只想把它弄干净。你知道怎么清洗RandomAccessFile吗?我知道的唯一方法就是这样做:
File file = new File("path to a sumFile");
file.delete();然后创建一个具有偶数名称的新文件。
发布于 2010-03-11 05:33:08
您可以使用RandomAccessFile.setLength(0)截断该文件。
发布于 2010-03-11 05:33:58
如果您所说的“清理”是指擦除内容并重用文件,那么您可以尝试将随机访问文件的FileChannel截断为零,然后将其设置回原来的大小。
https://stackoverflow.com/questions/2420736
复制相似问题