[英]Why does this throw java.nio.file.InvalidPathException?
The program throws程序抛出
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
When I try to get a path of a file with当我尝试获取文件的路径时
Path path = Paths.get("E:\\New folder");
why is that?这是为什么? Are drive letters allowed when getting paths?
获取路径时是否允许驱动器号?
The „:“ is „at index 2“, that is the third character of the string. „:“ 是„at index 2“,即字符串的第三个字符。 The drive letter should be at index 0, and the „:“ should be at index 1.
驱动器号应该在索引 0 处,“:”应该在索引 1 处。
There is a zero-width character in your string.您的字符串中有一个零宽度字符。 You can find the character by moving the cursor letter by letter with the arrow keys.
您可以通过使用箭头键一个字母一个字母地移动光标来找到该字符。 The character is where the cursor seems not to react to the keypress.
字符是光标似乎对按键没有反应的地方。
I see this happening when I copy a file path from Windows' “file properties” dialog, security page, „object name“, when I select the path with the mouse from end to beginning , there is a non-printing unicode “left-to-right embedding” character (U+202A) before the drive letter.当我从 Windows 的“文件属性”对话框、安全页面、“对象名称”复制文件路径时,我看到这种情况发生,当我用鼠标从头到尾选择路径时,有一个非打印的 unicode“left-驱动器号前的“向右嵌入”字符 (U+202A)。 It is not visible even with ¶ enabled (I tried Notepad++ and Eclipse).
即使启用了¶它也不可见(我尝试过 Notepad++ 和 Eclipse)。 This does not happen when I select the path from beginning to end.
当我从头到尾选择路径时,这不会发生。
Credit to Slaw who pointed in the right direction in his comment.感谢 Slaw ,他在评论中指出了正确的方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.