gpt4 book ai didi

java - Java 路径中的星号

转载 作者:搜寻专家 更新时间:2023-11-01 03:04:27 24 4
gpt4 key购买 nike

我正在阅读 OCA/OCP Java SE & Programmer I & II 学习指南。在第 520 页上有一种用于匹配的方法:

public void matches(Path path, String glob){
PathMatcher matcher = FileSystems.getDefault().getPathMatcher(glob);
System.out.println(matcher.matches(path));
}

现在,在下一页中有一段使用此方法的代码:

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);
}

根据书上的结果应该是:真的错误的错误的假的

但是,当我尝试运行它时,出现以下异常:

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

我的问题是:我是否遗漏了设置中的某些内容?我需要启用某些特定功能才能运行它吗?还是这本书错了?我只是在为 Java Professional 考试而学习,所以我不确定所有事情。

最佳答案

在 Windows 操作系统上,字符 * 不允许用于有效的文件和目录名称。 Microsoft Developer Network 上列出的字符也是如此:

You can use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following reserved characters:

  • < (less than)
  • > (greater than)
  • : (colon)
  • " (double quote)
  • / (forward slash)
  • \ (backslash)
  • | (vertical bar or pipe)
  • ? (question mark)
  • * (asterisk)
  • Integer value zero, sometimes referred to as the ASCII NUL character.
  • Characters whose integer representations are in the range from 1 through 31, except for alternate data streams where these characters are allowed. For more information about file streams, see File Streams.
  • Any other character that the target file system does not allow.

对于 UNIX,请参阅 Wikipedia .在 POSIX 上,允许的字符包括 A–Z a–z 0–9 。 _ -,保留为/null

可能是在允许 * 的操作系统上运行的示例。

关于java - Java 路径中的星号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27522581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com