目录中的Java NIO搜索文件

时间:2018-12-11 08:46:31

标签: java file nio glob

我想使用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;
            }
        });
    }

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

if (pathMatcher.matches(path)) {
    return path.toString();
}

3 个答案:

答案 0 :(得分:3)

有两件事需要更改:

对于“ 使用给定的glob字符串查找(第一个)文件”,如果遇到文件,则需要完成遍历树,因此如果匹配。并且您需要存储匹配路径作为结果。 Files.walkFileTree本身的结果是“起始文件”(JavaDoc)。那是一个指向Path的{​​{1}}。

location

如果没有匹配项,则结果public static String match(String glob, String location) throws IOException { StringBuilder result = new StringBuilder(); 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)) { result.append(path.toString()); return FileVisitResult.TERMINATE; } return FileVisitResult.CONTINUE; } }); return result.toString(); } 为空。

编辑:
使用String,我们仍然可以使用基于全局表达式的匹配器以更少的代码实现搜索:

Files.walk

结果public static Optional<Path> match(String glob, String location) throws IOException { PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher(glob); return Files.walk(Paths.get(location)).filter(pathMatcher::matches).findFirst(); } 显示可能不存在匹配项。

答案 1 :(得分:3)

根据您自己的代码。找到匹配项后就停止遍历

public class Main {

    private static Path found = "nothing"; // First file found

    public static void main(String[] args) throws IOException {
        String glob = "glob:**.{java}"; //pattern to look for
        String location = "D:\\"; //where to search
        match(location, glob);
        System.out.println("Found: " + found);
    }

    public static void match(String location, String glob) 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)) {
                    found = path; // Match found, stop traversal
                    return FileVisitResult.TERMINATE;
                }
                return FileVisitResult.CONTINUE;
            }
        });
    }
}

或者您可以填充集合并保存所有与模式匹配的文件

答案 2 :(得分:0)

我不知道此示例是否可以进一步帮助您,但是似乎您需要自己的自定义文件访问者。这是另一种解决方案:

proxy a

这是您正在运行的示例的另一种可能性:

package com.jesperancinha.files;

import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

public class GlobFileVisitor extends SimpleFileVisitor<Path> {
    private final PathMatcher pathMatcher;

    private Path path;

    public GlobFileVisitor(String glob) {
        this.pathMatcher = FileSystems.getDefault().getPathMatcher(glob);
    }

    @Override
    public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) {
        if (pathMatcher.matches(path)) {
            this.path = path;
            return FileVisitResult.TERMINATE;
        }
        return FileVisitResult.CONTINUE;
    }

    public Path getFoundPath() {
        return path;
    }
}