有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java nio路径无法处理windows网络路径

为什么会发生这种情况

def path=java.nio.file.Paths.get("c:/kittuhomestore/Csmart/files/companies");
path.getNameCount();
4

def path=java.nio.file.Paths.get("//kittuhomestore/Csmart/files/companies");
path.getNameCount();
2

后者是windows共享网络驱动器


共 (1) 个答案

  1. # 1 楼答案

    Path path = java.nio.file.Paths.get("c:/kittuhomestore/Csmart/files/companies");
    System.err.println(path.getRoot());
    

    输出:

    c:\
    

    在第一种情况下,路径的RootC:\,因此剩余部分是kittuhomestoreCsmartfilescompanies,因此是4组件


    Path path = java.nio.file.Paths.get("//kittuhomestore/Csmart/files/companies");
    System.err.println(path.getRoot());
    

    输出:

    \\kittuhomestore\Csmart\
    

    在第二种情况下,路径的Root\\kittuhomestore\Csmart\,因此剩余部分是filescompanies,因此是2组件

    这是因为UNC path具有以下格式

    \\server\share\file_path
    

    其中\\server\share是路径的根