How do I find the last modified file in a directory in java?
You can retrieve the time of the last modification using the File.lastModified() method. My suggested solution would be to implement a custom Comparator that sorts in lastModified()-order and insert all the Files in the directory in a TreeSet that sorts using this comparator.
Untested example:
SortedSet modificationOrder = new TreeSet(new Comparator() {
public int compare(File a, File b) {
return (int) (a.lastModified() - b.lastModified());
}
});
for (File file : myDir.listFiles()) {
modificationOrder.add(file);
}
File last = modificationOrder.last();
The solution suggested by Bozho is probably faster if you only need the last file. On the other hand, this might be useful if you need to do something more complicated.