【问题标题】:SimpleFileVisitor to walk a directory tree to find all .txt files except in two sub directoriesSimpleFileVisitor 遍历目录树以查找除两个子目录之外的所有 .txt 文件
【发布时间】:2019-04-14 17:07:47
【问题描述】:

我想遍历一个包含许多子目录的目录树。我的目标是打印除 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 语法有什么问题吗?

【问题讨论】:

标签: java nio simplefilevisitor


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 2010-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多