小编PSi*_*ngh的帖子

Java NIO 在目录中搜索文件

我想使用 Java NIO 和 glob 在特定目录中搜​​索文件(不知道全名)。

public static void match(String glob, String location) throws IOException {

        final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher(
                glob);

        Files.walkFileTree(Paths.get(location), new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path path,
                    BasicFileAttributes attrs) throws IOException {
                if (pathMatcher.matches(path)) {
                    System.out.println(path);
                }
                return FileVisitResult.CONTINUE;
            }
        });
    }
Run Code Online (Sandbox Code Playgroud)

阅读一些教程我这样做了。如果我找到(第一个)具有给定 glob 字符串的文件,我只想返回字符串。

if (pathMatcher.matches(path)) {
    return path.toString();
}
Run Code Online (Sandbox Code Playgroud)

java nio glob file

5
推荐指数
1
解决办法
2527
查看次数

标签 统计

file ×1

glob ×1

java ×1

nio ×1