[英]Why do I need to use Paths.get() to get a Path? Why aren't there .delete(), etc methods on it?
[注意:自我回答的问题 - 希望这会对你有所帮助]
我需要做Paths.get("/some/path/here")
来创建一个Path
; 为什么没有new Path(...)
构造函数? 事实上,为什么Path
是一个接口而不是一个类?
另外,为什么我需要调用Files.delete(somePath)
? 为什么没有path.delete()
?
为什么我需要使用Paths.get()来获取Path?
这是因为在Java 7中,您可以拥有多个FileSystem
; 每个FileSystem
是由一个提供FileSystemProvider
和由一个或多个FileStore
秒。 它是FileSystem
,其procudes Path
秒。
Paths.get(someStringHere)
实际上相当于:
FileSystems.getDefault().getPath(someStringHere)
为什么没有.delete()等方法呢?
因为删除/创建路径的逻辑,确定其类型已被移动到FileSystemProvider
。 因此, Files
中的绝大多数方法都是Path
和它们发出的FileSystemProvider
之间的网关。
这允许例如Files.copy()
从本地文件系统发布到另一个的Path
,例如,由提供对FTP服务器的访问的FileSystem
发出。 甚至是ZIP文件!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.