How to get the file with the latest last updated date in Java?

Possible duplicate:
How to find the last modified file in a directory in Java?

I have a directory of files that I need to check for changes. I believe that this changed when one of the files has modifiedDate newer than the one I remember from the last check (this means that it is a cache dependency).

What would be the fastest way to find the last modified file in a Java directory?


I may be too optimistic, but I'm clearly looking for something that does not require iterating all the files.

In addition, checking the date the directory was changed is insufficient, since it only changes when the list of files inside changes, and not when one of the files themselves has just been changed.

+6
java file
source share
4 answers

Even if you do not want to iterate over all files, any third-party API that you use to search for it will iterate over all files.

JDK 7 is for java.nio.WatchService, which will apparently go through everything if necessary, but will use file system support to accomplish exactly what you requested. On the negative side which is still in the future .

+2
source share

You should iterate over all the file names and extract the date of the change.

+1
source share

Check out this solution: How to get only the last 10 modified files from a directory using Java?

This is pure Java code, but with a simple hack to improve performance.

+1
source share

I think that this is impossible. You can use the Runtime class to execute the unix command line from java "ls -lrt | tail -1" and parse the string to get the name of the last modified file

0
source share

All Articles