1

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

I am trying to retrieve files from a directory in the order in which they were created. This has become an issue using Java. The listFiles() method in the File class does not guarantee an order in which the files are returned. Are there any other ways of retrieving the files by the order in which they were created in the directory?

Community
  • 1
  • 1
Reid Mac
  • 2,411
  • 6
  • 37
  • 64

5 Answers5

1

You would need to sort your array of files returned by File.listFiles() using a Comparator on the basis of File.lastModified

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • Just beat me to it! Only works if the files have not been modified since creation, of course... – DNA Nov 07 '12 at 21:22
  • I am already doing that, is the last modified date a reliable source for created date when files are being ftp'd to this folder? – Reid Mac Nov 07 '12 at 21:22
  • What is `ftp'd`? And did you try using it? Is it giving you correct result? – Rohit Jain Nov 07 '12 at 21:23
  • My program monitors a folder and needs to process the files in FIFO order. The files are transferred to that folder via File Transfer Protocol (FTP) – Reid Mac Nov 07 '12 at 21:24
  • @ReidMac. In that case, you would have to use the `creation time`. Modified time will not work for newly created files. – Rohit Jain Nov 07 '12 at 21:35
  • @ReidMac. Check [Files.readAttributes](http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#readAttributes%28java.nio.file.Path,%20java.lang.Class,%20java.nio.file.LinkOption...%29) and [BasicFileAttributes.creationTime](http://docs.oracle.com/javase/7/docs/api/java/nio/file/attribute/BasicFileAttributes.html#creationTime%28%29) for this purpose. – Rohit Jain Nov 07 '12 at 21:37
  • I have to use Java 6. Java 7 is not an option. – Reid Mac Nov 07 '12 at 21:49
  • [This](http://www.mkyong.com/java/how-to-get-the-file-creation-date-in-java/) may be useful – Miserable Variable Nov 07 '12 at 21:52
  • Yes sorting is not the problem. The problem is that files are being constantly added to this folder 24 hours a day 7 days a week. Whenever I retrieve files using the listFiles() method, is it possible that File A was inserted into the folder first, but not even retrieved using the listFiles() method while other files are retrieved, which were added later? – Reid Mac Nov 07 '12 at 21:53
  • It also needs to be OS independent. – Reid Mac Nov 07 '12 at 21:54
1

No, it does not, but the Arrays.sort method does take a Comparator. I've defined a simple one for you below and knock yourself out:

new Comparator<File>() {
public int compare(File m1, File  m2) {
    return m1.lastModified().compare(m2.lastModified());
}
});

Hope this helps...

hd1
  • 33,938
  • 5
  • 80
  • 91
0

Create A Set<File> unsorted where you add all the Files (unsorted.addAll(dir.listFiles()) Then, create a new TreeSet using Set<File> sorted=new TreeSet(Comparator<File> byDate) with a custom comparator comparing the dates.

Comparator<File> byDate=new Comparator<File>() {

    @Override
    public int compare(File o1, File o2) {
        return new Date(o1.lastModified()).compareTo(new Date(o2.lastModified()));
    }
};

and do:

sorted.addAll(unsorted);

The new Set will be ordered by date.

thedayofcondor
  • 3,860
  • 1
  • 19
  • 28
0

As you already figured out listFiles does not guarantee and order. As explained in this question you can get its attributes to find creation time and use something like thia to sort it.

Community
  • 1
  • 1
Miserable Variable
  • 28,432
  • 15
  • 72
  • 133
0

If you look in the Java API for File, there's a method called lastModified. If you sort the files by that attribute you should get what you wanted. I'm pretty sure there's no way to arrange files after their creation, since each implementation is OS specific.

So if you made a comparator like so:

public class FileComparator implements Comparator<File> {
  public int compare(File x, File y) {
    Long.compare(x.lastModified(), y.lastModified());
  }
}

It can be used like so:

File file = new File("...");
List<File> files = Arrays.asList(file.listFiles());
Collections.sort(files, new FileComparator());
Jens Egholm
  • 2,730
  • 3
  • 22
  • 35