gpt4 book ai didi

java - 如何理解java Path.toString()方法

转载 作者:行者123 更新时间:2023-12-02 10:13:44 25 4
gpt4 key购买 nike

我的代码:

Path uploadPath = Paths.get("D:\\0.hzg\\");
System.out.println(uploadPath);

输出:

D:\0.hzg

这里如何理解PathtoString()方法的设计?

我认为它应该输出 D:\0.hzg\ 因为 0.hzg 是路径而不是文件。

<小时/>

更新:

        System.out.println(Paths.get("-","A", "B")); 
System.out.println(Paths.get("/","A", "B"));
System.out.println(Paths.get("\\","A", "B"));

输出:

-\A\B
\\A\B\
\\A\B\

最佳答案

Path 不会执行任何 I/O 来检查这是目录还是常规文件。它是一个实用程序类,用于处理文件系统路径的平台相关字符串表示形式(您绝对不希望通过 / 分割字符串或自己查找驱动器号)。引用javadoc https://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html#toString()

An object that may be used to locate a file in a file system. It will typically represent a system dependent file path.

此外,不考虑尾随分隔符(例如,参见 endsWith 方法描述)

Note that trailing separators are not taken into account, and so invoking this method on the Path"foo/bar" with the String "bar/" returns true.

关于java - 如何理解java Path.toString()方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54820977/

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