From the book of "Kathy Sierra Bert Bates" for 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:**");
}
}
Output:
false
true
false
true
I can not understand the output clearly. Would any one explain me. let me know my example what is crossing directory boundaries. Thanks Rocky
matches(path, "glob:*.java"); // => flase
because your path contains /
which describe a directory hierarchy, *.java
matches any file name with .java
extension
matches(path, "glob:**/*.java"); // => true
because **
matches any string, including sub-path (like /com/java/
in you example)
matches(path, "glob:*"); // => false
as mentioned in the first one, because you have path separator /
matches(path, "glob:**"); // => true
as mentioned in the second one, because **
matches any string including /
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 are calling matches with the path as "/com/java/One.java"
and glob regular expression for searching or matching the path the function will take the values and perform the operation and return true or false.
Output:
true
false
true
true
If you are using the windows platform then you need to modify 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:**");
}
}
For more details Click here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With