Java file.list () sequential order?

This is a terrible question (in my opinion), and I tried my best to find useful documentation with little luck - anyway, here goes:

I have code that should perform some operation for all files in a directory. I set the directory to File and used fileObject.list () to iterate over the files in the directory. I left this code for the night, and it (after long breaks) crashed at some point. I'm trying to figure out at what point this happened (yes, I had a terrible registration). Now according to this javadoc, there is no guarantee of order (in alphabetical order or otherwise) when listing files, I wonder if there is any guarantee of consistency? Sense, when I run the same code twice, can I get the exact same file order? Logic tells me that this is necessary, and I resumed operations based on this, but I am suspicious of this. Also, I am curious that from javadoc "there is no specific order."

+5
source share
4 answers

This language means that you should not rely on any property of the order, including consistency from launch to launch.

, , . , , .

, -

Arrays.sort(
   myFileArray,
   new Comparator<File>() {
     public int compare(File a, File b) {
       return a.getName().compareTo(b.getName());
     }
   });
+13

, , , , , , .

0

, AFAIK " ", , , - , , . , "" , , , .

For common sense, however, I would recommend that you sort them when you receive them, if you rely on consistency.

0
source

As the documentation says, you cannot expect file ordering. In particular, two subsequent calls may return different orders. If you want to specify a specific order, for example. in alphabetical order, you need to sort the list yourself.

0
source

All Articles