我的代码:
Path uploadPath = Paths.get("D:\\0.hzg\\");
System.out.println(uploadPath);
输出:
D:\0.hzg
如何理解toString()
Path
方法的设计?
我认为它应该输出D:\0.hzg\
,因为0.hzg
is是路径而不是文件。
更新:
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\
[Paths]获取方法仅通过注意系统文件分隔符创建路径,这将有助于跨程序的跨平台执行,例如
System.out.println(Paths.get(File.separator,"A", "B"));
将返回
在Windows上:\\ A \ B \
在Unix上:/ A / B /
File.separator的详细信息是
系统相关的默认名称分隔符。此字段初始化为包含systemproperty file.separator值的第一个字符。在UNIX系统上,thisfield的值为'/';在Microsoft Windows系统上它是'\'。
具体问题问题
我相信这是因为绝对之路 例如在Windows JDK 1.7上
System.out.println(Paths.get("C:","A", "B")); **Output** : C:\A\B
System.out.println(Paths.get("C:","A", "0.hzg")); **Output** : C:\A\0.hzg
System.out.println(Paths.get(File.separator,"A", "B")); **Output** : \\A\B\
System.out.println(Paths.get(File.separator,"A", "0.hzg")); **Output** : \\A\0.hzg\
希望有所帮助
Path
没有做任何I/O
来检查这是一个目录还是一个普通文件。它是一个实用程序类,用于处理文件系统路径的平台相关字符串表示(您绝对不希望通过/
拆分字符串或自己查找驱动器号)。引用javadoc https://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html#toString()
可用于在文件系统中查找文件的对象。它通常表示系统相关的文件路径。
此外,不考虑尾随分隔符(参见例如endsWith
方法说明)
请注意,不考虑尾随分隔符,因此在路径“foo / bar”上使用字符串“bar /”调用此方法将返回true。