Will the value of File.lastModified always increase?

I am trying to control a file for changes in Java running on Windows or Linux.

I'm currently looking for polling the last modified file property instead of connecting to OS file events, trying to avoid smoothing API differences and handling asynchronous events.

I save the result of new File("path").lastModified() after processing the file and comparing the new lastModified results every x seconds.

Can I rely on the fact that, without allowing a foul, and someone manually modifies the timestamp, will lastModified always increase? Java docs say this is an offset from the GMT era, so apparently it won’t return even when setting time zones?

+4
source share
2 answers

You can change the timestamp of the file to all

http://unixhelp.ed.ac.uk/CGI/man-cgi?touch

If the time is corrected using something like NTP, it may go back.

These situations are rare, and you may have nothing to worry about.

+1
source

Well, sometimes lastModified does not increase. This is a temporary resolution, and in each file system the resolution is different:

  • all FAT (FAT32, FAT16, FAT12) have a temporary resolution of 2 second ,
  • for NTFS it is 100 ns (yep, nanoseconds)
  • for most * nixes its 1 second .

So, if your files change quickly, lastModified will not change, and your monitor may lose some changes.

This is also very difficult, because if you have the same file on different file systems (for example, FAT32 and NTFS), their lastModified time will differ due to different time resolutions. Now because of this, you can react to fake changes to files that were not there - it's just FAT32 , which returns a time that is 1 second different from NTFS time. This also applies to web servers - if you do not know the file system of the server, then you do not know what temporary resolution matters in the HTTP header.

The workaround is to assume that the file has not changed if the difference between the saved value and the FS value is less than 3-4 seconds. This is what WebStart does when checking for new versions of JAR files on the server.

+5
source

All Articles