gpt4 book ai didi

java - Java 8 中的深度优先目录流

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:39:53 25 4
gpt4 key购买 nike

我想使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com