在 Java 中截断文件的最佳实践方法是什么?例如这个虚拟函数,只是作为一个例子来阐明意图:
void readAndTruncate(File f, List<String> lines)
throws FileNotFoundException {
for (Scanner s = new Scanner(f); s.hasNextLine(); lines.add(s.nextLine())) {}
// truncate f here! how?
}
由于该文件充当占位符,因此无法删除该文件。