About Question enthuware.ocpjp.v8.2.1216 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
javalass
Posts: 54
Joined: Thu Mar 03, 2016 1:26 pm
Contact:

About Question enthuware.ocpjp.v8.2.1216 :

Post by javalass »

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.

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);
}
 
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.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests