java - 为什么这会抛出 java.nio.file.InvalidPathException?
问题描述
程序抛出
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");
这是为什么?获取路径时是否允许驱动器号?
解决方案
“:”是“在索引 2”,即字符串的第三个字符。驱动器号应位于索引 0,“:”应位于索引 1。
您的字符串中有一个零宽度字符。您可以通过使用箭头键逐个字母移动光标来查找字符。该字符是光标似乎对按键没有反应的地方。
当我从 Windows 的“文件属性”对话框、安全页面、“对象名称”复制文件路径时,我看到这种情况发生了,当我用鼠标从头到尾选择路径时,有一个非打印的 unicode “left-驱动器号之前的“右嵌入”字符 (U+202A)。即使启用它也不可见¶(我尝试过 Notepad++ 和 Eclipse)。当我从头到尾选择路径时,不会发生这种情况。
感谢 Slaw在评论中指出了正确的方向。
推荐阅读
- android - ACTION_CANCEL 不适用于非滑动 ViewPager?
- html - HTML 中的 img 元素:file 属性和 src 属性的行为不同
- javascript - 什么是索引之间连接的优雅方式
- javascript - 在 html 按钮中调用 javascript 函数时出错
- javascript - 我怎么知道是什么触发了变化?
- symfony - 开发期间使用 symfony 缓存问题
- image - 如何标准化构图差异很大的图像之间的颜色(从命令行)
- python - 如何防止2个线程覆盖值?
- html - 如何在 Umbraco 的源代码中添加 CSS 代码?
- android - 如何使用 google-cloud-bigquery 库从 android 运行 BigQuery 查询