java - SimpleFileVisitor 遍历目录树以查找除两个子目录之外的所有 .txt 文件
问题描述
我想遍历具有许多子目录的目录树。我的目标是打印除 subdir 和 anotherdir 子目录中的所有 .txt 文件。我可以使用下面的代码来实现这一点。
public static void main(String[] args) throws IOException {
Path path = Paths.get("C:\\Users\\bhapanda\\Documents\\target");
Files.walkFileTree(path, new Search());
}
private static final class Search extends SimpleFileVisitor<Path> {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
PathMatcher pm = FileSystems.getDefault().getPathMatcher("glob:**\\subdir");
PathMatcher pm1 = FileSystems.getDefault().getPathMatcher("glob:**\\anotherdir");
if (pm.matches(dir) || pm1.matches(dir)) {
System.out.println("matching dir found. skipping it");
return FileVisitResult.SKIP_SUBTREE;
} else {
return FileVisitResult.CONTINUE;
}
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
PathMatcher pm = FileSystems.getDefault().getPathMatcher("glob:*.txt");
if (pm.matches(file.getFileName())) {
System.out.println(file);
}
return FileVisitResult.CONTINUE;
}
}
但是,当我尝试将 pm 和 pm1 PathMatchers 与以下代码组合时,它不起作用。
PathMatcher pm = FileSystems.getDefault().getPathMatcher("glob:**\\{subdir,anotherdir}");
if (pm.matches(dir)) {
System.out.println("matching dir found. skipping it");
return FileVisitResult.SKIP_SUBTREE;
} else {
return FileVisitResult.CONTINUE;
}
}
glob 语法有什么问题吗?
解决方案
是的,glob 语法有问题。您需要将每个反斜杠加倍,以便它们在您的 glob 模式中保持转义的反斜杠。
第一个匹配器:
PathMatcher pm = FileSystems.getDefault().getPathMatcher("glob:**\\subdir");
与以 结尾的路径不匹配\subdir
。相反,双斜杠变成了 glob 模式中的单斜杠,这意味着“s”正在被转义。由于转义的 's' 只是一个 's',所以这个匹配器等价于:
PathMatcher pm = FileSystems.getDefault().getPathMatcher("glob:**subdir");
这意味着它将匹配任何以subdir
. 所以它会匹配路径xxx\subdir
,但也会匹配路径xxx\xxxsubdir
和xxxsubdir
。
组合匹配器:
PathMatcher pm = FileSystems.getDefault().getPathMatcher("glob:**\\{subdir,anotherdir}");
有同样的问题。在这种情况下被转义的是'{'。在 glob 模式中,这意味着将 '{' 视为文字字符而不是模式组的开头。所以这个匹配器不会匹配路径xxx\subdir
,但会匹配路径xxx{subdir,anotherdir}
。
这两个匹配器将执行预期的操作:
PathMatcher pm = FileSystems.getDefault().getPathMatcher("glob:**\\\\subdir");
PathMatcher pm = FileSystems.getDefault().getPathMatcher("glob:**\\\\{subdir,anotherdir}");
推荐阅读
- javascript - Display updated time each seconds in react function
- r - 来自给定范围的 R 独特组合快速且使用更少的系统资源
- reactjs - How to properly Handle a state by user typing and timeout Reactjs
- python - 如何在 PySpark 的成对 RDD 中使用转换?
- excel - 循环浏览工作表时出错。返回对象变量或未设置块变量
- django - 如何返回查询集和字典
- c++ - CMake 与需要与项目一起构建的第 3 方库
- flutter - 为什么标签栏小部件 Navigator.pop(context) 返回黑屏?
- c++ - 使用指针返回一个新数组
- ios - 如何将 [UILabel] 的集合转换为字符串文本以显示在 ViewController 上?