File's last modified time is changed only when the file is closed.
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()));
}
}
Now, in my actual program, a file is opened and one of the thread keeps writing the file. several other threads need to know when was any data last written to the file.
Is there any possible way update last modified without closing the file?
( I know, having a static
variable - long lastWriteTime
in the thread that writes the file would work. but just curious to know if there is any other way, to change the last modified time without closing the file. )