java8 的files、path类相关文件遍历API

Path的两种初始化(应该还有别的方式)

Path file = new File(path).toPath();

Paths.get

判断是文件、是目录

Files.isRegularFile(file)

Files.isDirectory(file)

javadoc说,还有既不是文件也不是目录的情况

Files.find

通过属性和路径筛选,可以筛选是文件,而路径匹配 PathMatcher 样式的。深度可以指定 integer max,无尽深度,或者限定1、2级深度。

PathMatcher 通过 FileSystem.getPathMatcher(String syntaxAndPattern) 获取。按javadoc介绍,有glob和regex两种方式。使用了glob的,没有使用regex的。 glob的支持 {*.gz,*.json} 这样的匹配方式。

还有 Files.walk, directorystream的api调用。这三个api可以遍历目录树,比file listfiles手写递归用高效多了。

返回的流,可以使用skip、limit,进行分页,然后调用foreach,对结果遍历处理。

原文地址:https://www.cnblogs.com/silvestris/p/12037592.html