我想使用 Stream API 以深度优先顺序遍历 Java 8 中的目录结构。这样做的原因是我想根据每个目录中每个文件中存在的时间戳对文件中的内容进行排序。本质上我正在寻找类似于 Files#walk 的东西但对于目录。我怎样才能做到这一点?
最佳答案
使用 StreamEx轻而易举:
File root = new File("someFilePath");
StreamEx.ofTree(root, x -> StreamEx.of(x.listFiles(File::isDirectory)))
.map(File::getAbsolutePath) // or whathever you need to do with the folder
.forEach(System.out::println); // the same as previous line
关于java - Java 8 中的深度优先目录流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43571065/