程序抛出
Exception in thread "main" java.nio.file.InvalidPathException: Illegal char
<:> at index 2: E:\New folder
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
javaapplication10.ACDirectoryListenerServiceImpl.main(ACDirectoryListenerServiceImpl.java:93)
Java Result: 1
当我尝试使用
获取文件的路径时Path path = Paths.get("E:\\New folder");
那是为什么?获取路径时是否允许驱动器号?
答案 0 :(得分:0)
„:“是„at index 2“,即字符串的第三个字符。驱动器号应该在索引 0 处,“:”应该在索引 1 处。
您的字符串中有一个零宽度字符。您可以通过使用箭头键逐个字母移动光标来找到该字符。字符是光标似乎对按键没有反应的地方。
当我从 Windows 的“文件属性”对话框、安全页面、“对象名称”复制文件路径时,当我用鼠标选择路径从头到尾时,我看到了这种情况,在驱动器号之前有一个非打印的 unicode“从左到右嵌入”字符 (U+202A)。即使启用了 ¶(我尝试过 Notepad++ 和 Eclipse),它也不可见。当我从头到尾选择路径时,不会发生这种情况。
感谢 Slaw 在他的评论中指出了正确的方向。