Why there is no Path constructor in java.nio.files.Path?

烂漫一生 提交于 2020-02-24 04:38:28

问题


The Path class has no documented constructor, but one creates instances via. Paths.get( "...." ) which is a shorthand for FileSystems.getDefault().getPath( "..." ). So can someone explain this design decission?


回答1:


can someone explain this design decision?

It is because JSR 203 allows paths to be issued from more than one FileSystem, unlike File, which is always linked to the file system the JVM lives on. In JSR 203, this filesystem is called the default filesystem. You can get a reference to it using FileSystems.getDefault().

You use Paths.get() to get a path from the default filesystem, which is strictly equivalent to FileSystems.getDefault().getPath(). If you were to get a Path from another file system, you would use this particular file system's .getPath().

As a proof that a FileSystem can be for (nearly) anything, here are a few implementations over different sources:

  • in memory;
  • FTP;
  • SMB/CIFS;
  • Dropbox.

And there are a few others.



来源:https://stackoverflow.com/questions/28040971/why-there-is-no-path-constructor-in-java-nio-files-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!