文件的最后修改时间只有在文件关闭时才会改变。
public class Main {
public static void main(String[] args) throws IOException {
File f = new File("xyz.txt");
FileWriter fwr = new FileWriter(f);
System.out.println(f.lastModified());
fwr.write("asasdasdasd");
System.out.println(f.setLastModified(System.currentTimeMillis()));
fwr.flush();
System.out.println(f.lastModified());
fwr.close();
System.out.println(f.lastModified());
System.out.println(f.setLastModified(System.currentTimeMillis()));
}
}
现在,在我的实际程序中,打开了一个文件,其中一个线程继续写入该文件。其他几个线程需要知道上次将任何数据写入文件的时间。
有没有可能在不关闭文件的情况下更新上次修改的方法?(我知道,有一个static
变量 -long lastWriteTime
在写入文件的线程中会起作用。但只是想知道是否有任何其他方法,可以在不关闭文件的情况下更改上次修改时间。)