gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:08:40 25 4
gpt4 key购买 nike

为什么会发生这种情况?

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 共享网络驱动器。

最佳答案

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

输出:

c:\

第一种情况,路径的RootC:\,所以剩下的部分是kittuhomestoreCsmart 文件公司,因此有4个组件。

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

输出:

\\kittuhomestore\Csmart\

在第二种情况下,路径的Root\\kittuhomestore\Csmart\,所以剩下的部分是filescompany,因此 2 个组件。

这是因为 UNC path具有格式

\\server\share\file_path

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

关于java nio路径无法处理Windows网络路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22072424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com