Clean a RandomAccessFile
I have a code:
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();
}
As you can see, there are files and their indexes, which show where can I find necessary strings. This part of a program do the addition of integers located in files. Files of type "file_1" I can close and then delete, but files of type "sumFile" can't, because I use referenes on them:
tempFile = sumFile;
temp开发者_运维知识库Index = sumIndex;
If I knew how to clean a file, I would not create "sumFile"-files each time I need a sumFile. I would just clean it. Do you know, how to clean a RandomAccessFile???? The only way I know is to do so:
File file = new File("path to a sumFile");
file.delete();
and then create a new file with even name.
You can use RandomAccessFile.setLength(0) to truncate the file.
If by "clean" you mean erase contents and re-use the file, you can try truncating the FileChannel of the random access file to zero and then setting it back to the original size you had.
精彩评论