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");
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.
source share