Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“线程中的异常”;“主要”;java.nio.file.InvalidPathException:UNC路径缺少主机名:/\/_Java_Nio - Fatal编程技术网

“线程中的异常”;“主要”;java.nio.file.InvalidPathException:UNC路径缺少主机名:/\/

“线程中的异常”;“主要”;java.nio.file.InvalidPathException:UNC路径缺少主机名:/\/,java,nio,Java,Nio,我正在想办法解决我的问题。我正在使用java.nio。在Linux环境中执行path.get(“/”,“/”).toString()时,由于它是基于Linux的路径,所以工作正常。但当我在Windows环境中执行它时,它会给我以下错误 Exception in thread "main" java.nio.file.InvalidPathException: UNC path is missing hostname: /\/ at sun.nio.fs.WindowsPathPa

我正在想办法解决我的问题。我正在使用
java.nio
。在Linux环境中执行
path.get(“/”,“/”).toString()
时,由于它是基于Linux的路径,所以工作正常。但当我在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)
我知道这在Windows中不是有效的路径系统。有什么方法可以让我同时在Windows和Linux上工作吗

注:
在我们的应用程序中有很多硬编码的正斜杠

只有第一个参数应该是绝对的,即以路径分隔符(
/
\
开头)

如果第二个值可以是绝对值,即您希望忽略前面的路径,请使用:

Paths.get("/").resolve("/").toString()   // returns "\" on Windows
path.get(“/”,“/”)
毫无用处,所以我不知道您真正的用例是什么。但是,您不应该在代码中使用硬编码的文件分隔符

假设要获取文件系统的根目录,可以执行两项操作:

  • 如果
    $PWD=/home/blah
  • FileSystems.getDefault().getRootDirectories()

  • 如果您不需要根目录,
    Paths.get
    将使用
    File.separator

    构造一个
    Path
    ,嘿,如果其中任何一个答案对您有效,也许您可以通过接受其中一个来表示感谢。如果他们不为你工作,请随意评论原因。