Order of traversal in Files.walkFileTree

五迷三道 提交于 2019-12-06 18:28:08

问题


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



来源:https://stackoverflow.com/questions/10396649/order-of-traversal-in-files-walkfiletree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!