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