7

This is an awful question (in my mind) and I've tried my best to find myself useful documentation with little luck - anyway, here goes:

I have code which needs to do some operation on all files in a directory. I set up the directory in a File object and use fileObject.list() to iterate over the files in the directory. I left this code running overnight and it (after much chugging) crashed at some point. I'm trying to figure out at what point this happened (yes I had awful logging). Now, according to this javadoc there is no guarantee of an order (alphabetical or otherwise) when listing files, I'm wondering if there is any guarantee of consistency? Meaning when I run the same code twice, would I get the exact same order of files? Logic tells me it should and I've resumed operations based on that but I'm suspicious of this. Also, I'm curious on what 'no specific order means' from the javadoc.

kyun
  • 640
  • 4
  • 10
  • 1
    If you want to guarantee order, why not use a `Comparator`? – mre Jun 30 '11 at 18:50
  • Files might change while the app is running... so that could make things different on each run. The order is most likely dependent on the file system implementation. Most likely the answer is "yes, there is consistency" as if you do an "ls" or "dir" on a command line will give you consistency but no guaranteed alphabetical sorting. – ricosrealm Jun 30 '11 at 18:51
  • Ah yes, I would (now), but this is to resume a run which failed and me not wanting to repeat processed files. – kyun Jun 30 '11 at 18:51
  • @ricosrealm ls makes life easy by giving a default sort order. And for my use case this is a read-only directory, the files won't change – kyun Jun 30 '11 at 18:53

4 Answers4

15

That language means you should not rely on any property of the order including consistency from run to run.

If there is a linked list of files in some in-memory data structure, a driver might move the most recently accessed to the front of the list to optimize for repeated file access. This might change the order in which files are listed even though no file has been modified.

If you want a consistent order, you can do something like

Arrays.sort(
   myFileArray,
   new Comparator<File>() {
     public int compare(File a, File b) {
       return a.getName().compareTo(b.getName());
     }
   });
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • 1
    Thats worrying. And sort of what I expected. It gives me no way to know what files got processed and what didn't. With the awful logging that is. The sort makes sense, but would be useful if I wanted to start the whole thing over. Thanks though! – kyun Jun 30 '11 at 18:50
  • 4
    It might be worth looking into java 7's new `WatchService` API. http://stackoverflow.com/questions/494869/file-changed-listener-in-java/494957#494957 – Mike Samuel Jun 30 '11 at 18:53
0

Given that the files you are looking at do not change in between calls and given you run them on the exact same platform twice, i think you will get the same results.

mkro
  • 1,902
  • 14
  • 15
  • Is my hope as well - just wondering if there's documentation to that effect which will put my mind to ease – kyun Jun 30 '11 at 18:48
  • Basically, do not rely on consistency here. I like Mike's answer. – mkro Jun 30 '11 at 18:50
  • It that is so important, I would run the program again, but sorting out the files first and implement some log – woliveirajr Jun 30 '11 at 18:54
0

There is no guarantee of order, but AFAIK there is no "guarantee of inconsistency", so calling it twice, given that no files have been changed or touched in any way, will probably give you the same order. But, if you were "processing" them, they probably have been touched.

For sanity though, I'd recommend that you sort them when you get them if you're going to rely on consistency.

trutheality
  • 23,114
  • 6
  • 54
  • 68
  • :) I should've been clearer - I'm reading them, not editing them. So, the directory structure and files in it do not change. That said, as Mike Samuel points out, the file system will probably have a role to play on the order, meaning I might not get the exact same order. – kyun Jun 30 '11 at 18:58
  • You can always try and test if order is preserved. That's the simplest way to be sure if it's stable enough for you to continue the run from wherever it stopped. – trutheality Jun 30 '11 at 19:02
  • I did, and it seemed to be alright. But thats what led to the question, wondering if thats always true/ just lucky this time – kyun Jun 30 '11 at 19:05
0

As the documentation says you cannot expect any ordering of the files. In particular, two subsequent calls can return different orderings. If you wish a specific ordering, e.g. alphabetical, you need to sort the list by yourself.

Jiri Kriz
  • 9,192
  • 3
  • 29
  • 36