Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the file with latest lastModified date, in Java? [duplicate]

Possible Duplicate:
How do I find the last modified file in a directory in Java?

I have a directory of files that I need to check for changes. I consider it changed when one of the files has a modifiedDate newer than what I remembered from the last check (this is meant to be a cache dependency).

What would be the fastest way of finding the latest modified file in a directory in Java?


Maybe I'm too optimistic, but I'm explicitly looking for something that does not involve iterating all the files.

Also, checking the directory modified date is not enough, as this only changes when the list of files inside changes, not when one of the files themselves is just modified.

like image 752
Tomalak Avatar asked Jul 29 '10 13:07

Tomalak


1 Answers

Even if you're not looking to iterate all of the files, any third-party API you use to find this will iterate all of the files.

JDK 7 is intended to have java.nio.WatchService, which will apparently iterate through everything if it has to, but will use filesystem support to do exactly what you've asked for. On the minus side, that's still in the future.

like image 193
Dean J Avatar answered Oct 05 '22 23:10

Dean J