About Question enthuware.ocpjp.v8.2.1216 :
Posted: Tue Mar 29, 2016 4:39 pm
How come the glob syntax is different when using the version of newDirectoryStream() that takes a String? It seems like you don't need a double asterisk (**) to cross directory bounders, like you do when using a PathMatcher.
The output for the code above is the same, but the glob syntax is different. If I change the getPathMatcher argument to "glob:*.txt" (single asterisk), it no longer finds anything because it doesn't cross directory boundaries.
Code: Select all
Path path = Paths.get("plants");
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**.txt");
try (DirectoryStream<Path> usingString = Files.newDirectoryStream(path, "*.txt");
DirectoryStream<Path> usingMatcher = Files.newDirectoryStream(path, matcher::matches)) {
usingMatcher.forEach(System.out::println);
System.out.println("---------------------------------------");
usingString.forEach(System.out::println);
}