Java Reference
In-Depth Information
Note An important thing to note here is that Path is an interface type, whereas
Paths is a normal class (albeit a very simple one, without a public constructor).
In case you're wondering why the former is an interface, the reason for this is to
allow developers of custom file systems to be able to implement (or extend) it.
The usage of the s suffix of Path versus Paths is also in line with other
concepts in NIO2, for instance Files (which you'll encounter later on) and
FileSystem versus FileSystems . In fact, calling the get method of Paths is a
shorthand for:
FileSystems // A static utility class containing methods to create
FileSystem objects
.getDefault() // Get the default file system FileSystem
.getPath("C:\\projects\\outline.txt") // Return a Path
Once you have created a Path object, there are a number of methods you can execute on them to
retrieve information about the path. These methods do not require that the file corresponding to the
path actually exist:
String myPath.toString() : Returns the string representation of the Path object. Note
that this method will attempt to perform syntactic cleanup.
Path myPath.getFileName() : Returns the filename or the last element in the Path object.
Path myPath.getName(int i) : Returns the Path element corresponding to the specified
index. Note that index 0 does not represent the root, but the element closest to the root.
int myPath.getNameCount() : Returns the number of elements in the path.
Path myPath.subpath(int i, int j) : Returns the subsequence of the Path (not includ-
ing a root element) as specified by beginning and ending indices.
Path myPath.getParent() : Returns the Path of the parent directory of this path.
Path myPath.getRoot() : Returns the root of the path.
Path myPath.normalize() : Cleans up redundancies from a path and returns the cleaned-
up result. For example, “C:\.\projects\..\movies\vacation.avi” is converted to “C:\
movies\vacation.avi” .
Path myPath.resolve(String partialPath) : The partial path (not including a root ele-
ment) is added to the original path and the new path is returned. If you pass in an absolute
path, the absolute Path itself will be returned.
Path myPath.relativize(Path otherPath) : Constructs a new Path object originating
from the original path and ending at the location specified by otherPath . This returns a rela-
tive Path .
In addition, methods exist to convert a path:
URI myPath.toUri() : Converts the path to a string that can be opened by web browsers.
Search WWH ::




Custom Search