清理RandomAccessFile

Dmi*_*try 2 java

我有一个代码:

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();
}
Run Code Online (Sandbox Code Playgroud)

如您所见,有文件及其索引,它们显示了我在哪里可以找到必要的字符串.程序的这一部分添加位于文件中的整数."file_1"类型的文件我可以关闭然后删除,但是"sumFile"类型的文件不能,因为我在它们上使用referenes:

tempFile = sumFile;
tempIndex = sumIndex;
Run Code Online (Sandbox Code Playgroud)

如果我知道如何清理文件,每次需要sumFile时都不会创建"sumFile"文件.我会干净它.你知道吗,如何清理RandomAccessFile ???? 我知道的唯一方法是这样做:

File file = new File("path to a sumFile");
file.delete();
Run Code Online (Sandbox Code Playgroud)

然后使用偶数名称创建一个新文件.

vla*_*adr 14

您可以使用RandomAccessFile.setLength(0)截断文件.

  • @Dmitry你应该接受一个适合你的答案. (3认同)