问题
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