[英]java.nio.file.InvalidPathException when the path contains colon (:)
I need to construct a path which contains colon (:).我需要构建一个包含冒号 (:) 的路径。 Actually the path is not a folder/directory in windows/linux.
实际上,路径不是 windows/linux 中的文件夹/目录。 It is just a path of JCR repo.
这只是 JCR repo 的一个路径。 Below code gives InvalidPathException when the path contains colon
当路径包含冒号时,下面的代码给出 InvalidPathException
import java.nio.file.Path;
import java.nio.file.Paths;
public class TestCLass {
public static void main(String[] args) {
final Path path = Paths.get("com", "repo:access","resource");
//final Path path = Paths.get("com", "repoaccess","resource"); //Output - com/repoaccess/resource
System.out.println(path);
}
}
Exception例外
Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 8: com\repo:access\resource
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)
Is there any API to access colon(:) and any other special character?是否有任何 API 可以访问冒号 (:) 和任何其他特殊字符?
As far as I know, neither Windows nor Linux allow ":" to be put in a file name.据我所知,Windows 和 Linux 都不允许在文件名中添加“:”。 That's why Java throws
InvalidPathException
for such a path/file name.这就是 Java 为这样的路径/文件名抛出
InvalidPathException
的原因。 There's no API to work around a restriction imposed by the file system.没有 API 可以解决文件系统强加的限制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.