JAVA中的文件操作3-如何查找文件
JAVA中的文件操作3-如何查找文件
在前面的和中,我们介绍了文件的基本操作。那么,我们有时候还可能会遇到从文件夹中搜索某个文件的情况,这时候就需要对文件进行查找了。
通过java提供的Files.walk和PathMatcher可以来完成这一任务。Files.walk可以遍历目录树,PathMatcher可以完成目标文件的匹配,两者结合即可找到我们需要的文件。 如果还对文件有其他要求,比如最近更新时间,还可以通过Files.find来自定义过滤器来完成,不过一般都不需要。
PathMatcher支持两种查找模式:glob和regex,用法是FileSystems.getDefault().getPathMatcher(matcher),matcher的语法是: 模式:匹配串,比如:
FileSystems.getDefault().getPathMatcher("glob:*.txt");
glob模式匹配
glob模式起源于 Unix 的 bash shell,在shell中非常常用,在这里的用法也是一致的。
比如下面的示例,查找所有md文件:
Path rootPath = Paths.get("D:\project"); PathMatcher markdownFileMatcher = FileSystems.getDefault().getPathMatcher("glob:**/*.md"); try (Stream<Path> fileAndDirs = Files.walk(rootPath)) { fileAndDirs .filter(markdownFileMatcher::matches) .limit(10) .forEach(System.out::println); }
这里面的**表示任意子目录,*表示任意字符串。 使用try是因为Files.walk返回的是
下面是几个通配符的解释:
最常用的就是**和*了,请务必注意其中区别。
regex模式
regex模式即正则表达式模式,可以书写正则表达式来匹配文件,同样是查找md文件:
Path rootPath = Paths.get("D:\project"); PathMatcher markdownFileMatcher = FileSystems.getDefault().getPathMatcher("regex:.*\.md"); try (Stream<Path> fileAndDirs = Files.walk(rootPath)) { fileAndDirs .filter(markdownFileMatcher::matches) .limit(10) .forEach(System.out::println); }
Files.find查找文件
Files.find提供了自定义过滤器的方法,下面查找了最近修改时间在10天以内的文件:
Instant tenDaysBefore = Instant.now().minus(10, ChronoUnit.DAYS); BiPredicate<Path, BasicFileAttributes> matcher = (path, attrs) -> Files.isRegularFile(path) && attrs.lastModifiedTime().toInstant().isAfter(tenDaysBefore); try (Stream<Path> files = Files.find(rootPath, Integer.MAX_VALUE, matcher)) { files .limit(10) .forEach(System.out::println); }
matcher可以获取到Path示例来匹配路径或者文件内容,还可以获取到BasicFileAttributes得到文件的修改时间等信息,可以根据更新时间等信息来匹配文件。
下一篇:
JDK17上手,新特性让你起飞