[英]Exception in thread “main” java.nio.file.InvalidPathException: UNC path is missing hostname: /\/
I am trying to figure out a way to solve my problem. 我正在尝试找出解决问题的方法。 I am using
java.nio
. 我正在使用
java.nio
。 When i execute Paths.get("/","/").toString()
in Linux environment, it is working fine as it is Linux based path. 当我在Linux环境中执行
Paths.get("/","/").toString()
时,它工作正常,因为它是基于Linux的路径。 But when i execute it in Windows environment, it gives me the following error. 但是,当我在Windows环境中执行它时,它给了我以下错误。
Exception in thread "main" java.nio.file.InvalidPathException: UNC path is missing hostname: /\/
at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:113)
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)
I understand this is not a valid path system in Windows. 我了解这不是Windows中有效的路径系统。 Is there any way so that i can work for both Windows and Linux?
有什么办法可以使我同时在Windows和Linux上工作?
Note: There are lot of hard coded Forward Slash in our application. 注意:我们的应用程序中有很多硬编码的正斜杠。
Paths.get("/","/")
is trivially useless, so I've no idea what your real use case is. Paths.get("/","/")
没有用,所以我不知道您真正的用例是什么。 However, you should never need to use hard-coded file separator in your code. 但是,您永远不需要在代码中使用硬编码的文件分隔符。
Assuming you want to get the root directory of a file system, you could do 2 things: 假设您要获取文件系统的根目录,则可以做两件事:
Paths.get(".").getRoot()
would return /
if $PWD=/home/blah
$PWD=/home/blah
则Paths.get(".").getRoot()
将返回/
FileSystems.getDefault().getRootDirectories()
If you don't need the root directory, Paths.get
would construct a Path
using File.separator
. 如果不需要根目录,则
Paths.get
将使用File.separator
构造一个Path
。
Only the first parameter should be absolute, ie start with a path separator ( /
or \\
). 只有第一个参数应该是绝对的,即以路径分隔符(
/
或\\
)开头。
If the second value can be absolute, ie you want to ignore the preceding path, use: 如果第二个值可以是绝对值,即要忽略前面的路径,请使用:
Paths.get("/").resolve("/").toString() // returns "\" on Windows
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.