首页 > 解决方案 > 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 语法有什么问题吗?

标签: javaniosimplefilevisitor

解决方案


是的,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\xxxsubdirxxxsubdir

组合匹配器:

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}");

推荐阅读