Order of traversal in Files.walkFileTree
Asked Answered
T

2

15

What is the order in which Files.walkFileTree visits files/directories at the same level?

It doesn't appear to visit them in order of size, last modified time or name. I couldn't find anything in the API documentation either.

Perhaps the preVisitDirectory method can be used to specify the order of visit but what is default behaviour ?

Tapp answered 1/5, 2012 at 10:47 Comment(0)
A
21

The order in which the subdirectories are read is not defined as per this comment in the Java Tutorial:

A file tree is walked depth first, but you cannot make any assumptions about the iteration order that subdirectories are visited.

As for the order in which the files are read, it depends (in the current implementation) on the supplied DirectoryStream, which is sun.nio.fs.WindowsDirectoryStream on my computer. Reading the javadoc of DirectoryStream, you will see that:

The elements returned by the iterator are in no specific order.

Aurelioaurelius answered 1/5, 2012 at 10:54 Comment(2)
Thanks for the answer. So Files.walkTreeFile will at some point call Files.newDirectoryStream and the DirectoryStream returned depends on the os and/or the jdk/jre ?Tapp
Basically yes. The bottom line is: don't code assuming that you will get a specific order as there is no guarantee about it.Aurelioaurelius
B
2

java can sort it for you later, here is what I did.

public static void printy(Path rootDirPath) {
        //treesets to hold paths alphabetically
        TreeSet<Path> paths = new TreeSet<>();
        try {
            Files.walkFileTree(rootDirPath, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    paths.add(dir);
                    return super.preVisitDirectory(rootDirPath, attrs);
                }
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    paths.add(file);
                    return super.visitFile(rootDirPath, attrs);
                }
                @Override
                public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                    return super.visitFileFailed(file, exc);
                }
                @Override
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                    return super.postVisitDirectory(rootDirPath, exc);
                }
            });
        //I'm printing the contents alphabetically,.. your impl might vary
        paths.forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Hope this helps

Brandnew answered 23/3, 2018 at 6:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.