Order of traversal in Files.walkFileTree

2020-08-10 08:53发布

问题:

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 ?

回答1:

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.



回答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