6

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.

Community
  • 1
  • 1
Tomalak
  • 332,285
  • 67
  • 532
  • 628

4 Answers4

2

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.

Dean J
  • 39,360
  • 16
  • 67
  • 93
  • I already thought so. I guess this is what I will end up doing. – Tomalak Jul 29 '10 at 14:16
  • Actually, the future has something shiny. Let me edit my answer. – Dean J Jul 29 '10 at 15:04
  • Thanks for the edit. :-) PS: Something like this is doable in .NET already (via `System.IO.FileSystemWatcher`). – Tomalak Jul 29 '10 at 15:14
  • I'm very much looking forward to JDK 7 - if and when it's actually done - as Java has some catchup to do with things C# devs now take for granted. – Dean J Jul 29 '10 at 17:21
1

You must iterate all file names and extract the modification date.

darpet
  • 3,073
  • 3
  • 33
  • 40
1

See this solution: How to get only 10 last modified files from directory using Java?

It is pure java code but with a simple hack to improve performance.

Community
  • 1
  • 1
rahulmohan
  • 1,285
  • 11
  • 19
  • I'm not sure if I understand this solution right: The basic operations are "get each file, look at its date". These are no faster than in an old-fashioned loop. So the speed improvement comes from doing the on-the-fly sorting? – Tomalak Feb 16 '11 at 21:31
  • You are right. Its no faster than a loop, but there is only one loop. Any other portable solution (pure java, no escaping to OS specific commands etc.) would need 1 loop to get all the files and then another loop over the files to get the last modified one. This one is saving that second loop by putting that logic in the first loop itself. – rahulmohan Feb 17 '11 at 11:24
  • Thanks for clarifying. :) However, to be completely honest: This particular solution was meant to be used in ColdFusion context, which runs on top of Java. You can operate Java objects in ColdFusion, but it's impossible to implement interfaces or any other of the advanced stuff. I rather did not want to write my own Java class for this problem, so I left it at "do two loops". It's good enough for now. *PS: Use @ replies in comments or your responses might go unnoticed. (Only comments on the someone's own post, like me commenting on your answer, don't need an @ reply.)* – Tomalak Feb 17 '11 at 19:57
0

I think that is not possible. You can use an Runtime class to exec an unix command line from java "ls -lrt | tail -1" and parse the string to get the name of the latest modified file