从Oathy考试的"Kathy Sierra Bert Bates"一书中我发现了以下代码
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:**");
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
false
true
false
true
Run Code Online (Sandbox Code Playgroud)
我无法清楚地理解输出.有人会解释我吗?让我知道我的例子是什么是跨越目录边界.谢谢洛基
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.
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的程序中,当您调用与路径匹配的 as"/com/java/One.java"和 glob 正则表达式来搜索或匹配路径时,该函数将获取值并执行操作并返回 true 或 false。输出:
true
false
true
true
Run Code Online (Sandbox Code Playgroud)
如果您使用的是windows平台,那么您需要按如下方式修改您的程序。
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:**");
}
}
Run Code Online (Sandbox Code Playgroud)
欲了解更多详情,请点击这里
| 归档时间: |
|
| 查看次数: |
2040 次 |
| 最近记录: |