I want to search a file(without knowing the full name) in a specific directory using Java NIO and 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;
}
});
}
Reading some tutorial I did this. I just want to return string if I find(first) file with given glob String.
if (pathMatcher.matches(path)) {
return path.toString();
}