我正在阅读 OCA/OCP Java SE 和程序员 I 和 II 学习指南。第520页有一个用于匹配的方法:
public void matches(Path path, String glob){
PathMatcher matcher = FileSystems.getDefault().getPathMatcher(glob);
System.out.println(matcher.matches(path));
}
Run Code Online (Sandbox Code Playgroud)
现在,在下一页上有一段使用此方法的代码:
public static void main(String [] args){
Path path1= Paths.get("0*b/test/1");
Path path2 = Paths.get("9\\*b/test/1");
Path path3 = Paths.get("01b/test/1");
Path path4 = Paths.get("0*b/1");
String glob = "glob:[0-9]\\*{A*,b}/**/1";
matches(path1,glob);
matches(path2,glob);
matches(path3,glob);
matches(path4,glob);
}
Run Code Online (Sandbox Code Playgroud)
按照书上的说法结果应该是: true false false false
但是,当我尝试运行它时,出现以下异常:
Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <*> at index 1: 0*b/test/1
at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
at java.nio.file.Paths.get(Paths.java:84)
at pathmatcherobjective.PathMatcherObjective.main(PathMatcherObjective.java:63)
Java Result: 1
Run Code Online (Sandbox Code Playgroud)
我的问题是:我是否错过了设置中的某些内容?我需要启用某些特定功能才能运行它吗?或者这本书本身就是错误的?我正在准备 Java 专业考试,所以我对一切都不确定。
在 Windows 操作系统上,*有效的文件和目录名称不允许使用该字符。Microsoft Developer Network上列出的字符也是如此:
\n\n\n您可以使用当前代码页中的任何字符作为名称,\n 包括 Unicode 字符和扩展字符集 (128\xe2\x80\x93255) 中的字符,但以下保留字符除外:
\n\n\n
\n- <(小于)
\n- >(大于)
\n- : (冒号)
\n- “(双引号)
\n- /(正斜杠)
\n- \\(反斜杠)
\n- | (竖条或竖管)
\n- ?(问号)
\n- *(星号)
\n- 整数值零,有时称为 ASCII NUL 字符。
\n- 整数表示形式在 1 到 31 范围内的字符,允许使用这些字符的备用数据流除外。有关文件流的详细信息,请参阅文件\n 流。
\n- 目标文件系统不允许的任何其他字符。
\n
对于 UNIX,请参阅维基百科。\n在 POSIX 上允许的字符包括A\xe2\x80\x93Z a\xe2\x80\x93z 0\xe2\x80\x939 . _ -,保留的字符有/ null。
可能是在允许的操作系统上运行的示例*。
| 归档时间: |
|
| 查看次数: |
2891 次 |
| 最近记录: |