Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file.lastModified() function does not give the updated value

Tags:

java

file.lastModified() not updated , although I change the file content , I work on Windows.
I use File of java , what could be the problem ? Do I need to do some kind of refresh from the code ?

like image 398
user3086314 Avatar asked Dec 11 '25 09:12

user3086314


1 Answers

Java, at least 1.8 appears to be caching file attributes. I created a C++ application that uses the Win32's FindFirstFile to get the file information and verify that the last modified value is updated every time. And it does. But Java's File and Files both seem to not always get the latest value. Even tried a one second delay between writes and it didn't always get updated. I'm using Files.write to modify the file so I would hope it's closing it. Not sure if there's a good solution for this in Java, which is unfortunate.

UPDATE:

I found that Files.getLastModifiedTime is a solution for this. But it's only available for Java 1.8 or later.

like image 190
David Bradley Avatar answered Dec 14 '25 07:12

David Bradley