Glob template using getPathMatcher

From the Kathy Sierra Bert Bates book for the OCP exam, I found the following code

public class FileTest { public static void matches(Path path, String glob){ PathMatcher matcher = FileSystems.getDefault().getPathMatcher(glob); System.out.println(matcher.matches(path)); } public static void main(String[] args) throws IOException { Path path = Paths.get("/com/java/One.java"); matches(path, "glob:*.java"); matches(path, "glob:**/*.java"); matches(path, "glob:*"); matches(path, "glob:**"); } } 

Exit:

 false true false true 

I do not understand the report explicitly. Someone explain to me. let me know my example that crosses directory boundaries. thanks rocky

+5
source share
2 answers
 matches(path, "glob:*.java"); // => flase 

because your path contains / that describe the directory hierarchy, *.java matches any file name with a .java

 matches(path, "glob:**/*.java"); // => true 

because ** matches any string, including a subpath (e.g. /com/java/ in your example)

 matches(path, "glob:*"); // => false 

as mentioned in the first, because you have a path separator /

 matches(path, "glob:**"); // => true 

as indicated in the second, since ** matches any string containing /

+2
source
 public class FileTest { public static void matches(Path path, String glob){ PathMatcher matcher = FileSystems.getDefault().getPathMatcher(glob); System.out.println(matcher.matches(path)); } public static void main(String[] args) throws IOException { Path path = Paths.get("/com/java/One.java"); matches(path, "glob:*.java"); // regular expression that matches any file path that end with .java so it will return the value as true matches(path, "glob:**/*.java"); // regular expression ** characters matches zero or more characters crossing directory boundaries so it will match complete path but if you put /* it will search for a path like this /com/java//one.java soe here it will not match the path and will return value as false. matches(path, "glob:*"); // this will match any path and return value as true. matches(path, "glob:**"); // this will complete path crossing directory so it will return you value as true. } } 

In the above program, when you invoke matches with a path like "/com/java/One.java" and a glob regular expression to find or match a path, the function takes values ​​and performs the operation and returns true or false. Exit:

 true false true true 

If you are using the Windows platform, you need to change your program as follows.

 public class match { public static void matches(Path path, String glob){ PathMatcher matcher = FileSystems.getDefault().getPathMatcher(glob); System.out.println(matcher.matches(path)); } public static void main(String[] args) throws IOException { Path path = Paths.get("\\com\\java\\One.java"); matches(path, "glob:*.java"); matches(path, "glob:**\\*.java"); matches(path, "glob:*"); matches(path, "glob:**"); } } 

Click here for more details.

+1
source

Source: https://habr.com/ru/post/1210293/


All Articles