A Path represents a path that is hierarchical and composed of a
sequence of directory and file name elements separated by a special separator
or delimiter. A root component, that identifies a file system
hierarchy, may also be present. The name element that is farthest
from the root of the directory hierarchy is the name of a file or directory.
The other name elements are directory names. A Path can represent a
root, a root and a sequence of names, or simply one or more name elements.
A Path is considered to be an empty path if it consists
solely of one name element that is empty. Accessing a file using an
empty path is equivalent to accessing the default directory of the
file system. Path defines the getFileName,
getParent, getRoot, and subpath methods to access the path components or a subsequence of its name
elements.
In addition to accessing the components of a path, a Path also
defines the resolve and resolveSibling methods to combine paths. The relativize
method that can be used to construct a relative path between two paths.
Paths can be compared, and tested against each other using
the startsWith and endsWith methods.
This interface extends Watchable interface so that a directory
located by a path can be registered with a WatchService and entries in the directory watched.
Warning
This interface is only intended to be implemented by those developing custom file system implementations. Methods may be added to this interface in future releases.
Paths may be used with the Files class to operate on files,
directories, and other types of files. For example, suppose we want a java. to read text from a file "access.log". The
file is located in a directory "logs" relative to the current working
directory and is UTF-8 encoded.
Path path = FileSystems.getDefault().getPath("logs", "access.log"); BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
Paths associated with the default provider are generally interoperable
with the java. class. Paths created by other
providers are unlikely to be interoperable with the abstract path names
represented by java.io.File. The toPath
method may be used to obtain a Path from the abstract path name
represented by a java.io.File object. The resulting Path can
be used to operate on the same file as the java.io.File object. In
addition, the toFile method is useful to construct a File from the String representation of a Path.
Implementations of this interface are immutable and safe for use by multiple concurrent threads.
| Modifier and Type | Method and Description |
|---|---|
| public int | Returns: zero if the argument isequal to this path, a
value less than zero if this path is lexicographically less than
the argument, or a value greater than zero if this path is
lexicographically greater than the argumentthe path compared to this path. other)Redeclares java. Compares two abstract paths lexicographically. |
| public boolean | |
| public default boolean | Returns: true if this path ends with the given path; otherwise
falsethe given path string other)Tests if this path ends with a |
| public boolean | |
| public Path | Returns: a path representing the name of the file or directory, ornull if this path has zero elementsReturns the name of the file or directory denoted by this path as a
|
| public FileSystem | Returns: the file system that created this objectReturns the file system that created this object. |
| public Path | Returns: the name elementthe index of the element index)Returns a name element of this path as a |
| public int | Returns: the number of elements in the path, or0 if this path
only represents a root componentReturns the number of name elements in the path. |
| public Path | Returns: a path representing the path's parentReturns the parent path, or |
| public Path | Returns: a path representing the root component of this path, ornullReturns the root component of this path as a |
| public int | |
| public boolean | Returns: true if, and only if, this path is absoluteTells whether or not this path is absolute. |
| public default Iterator | Returns: an iterator over the name elements of this pathImplements java. Returns an iterator over the name elements of this path. |
| public Path | Returns: the resulting path or this path if it does not contain redundant name elements; an empty path is returned if this path does not have a root component and all name elements are redundantReturns a path that is this path with redundant name elements eliminated. |
| public static Path | |
| public static Path | |
| public WatchKey | Returns: a key representing the registration of this object with the given watch servicethe watch service to which this object is to be registered watcher, WatchEvent.the events for which this object should be registered events,the modifiers, if any, that modify how the object is registered modifiersRedeclares java. Registers the file located by this path with a watch service. |
| public default WatchKey | Returns: A key representing the registration of this object with the given watch serviceThe watch service to which this object is to be registered watcher, WatchEvent.The events for which this object should be registered eventsImplements java. Registers the file located by this path with a watch service. |
| public Path | Returns: the resulting relative path, or an empty path if both paths are equalthe path to relativize against this path other)Constructs a relative path between this path and a given path. |
| public Path | |
| public default Path | |
| public default Path | |
| public default Path | Returns: the resulting paththe first path string to convert to a path and
resolve against this path first, String... additional path strings to be iteratively converted to
paths and resolved more)Converts a path string to a path, resolves that path against this path, and then iteratively performs the same procedure for any additional path strings. |
| public default Path | Returns: the resulting paththe path to resolve against this path's parent other)Resolves the given path against this path's |
| public default Path | Returns: the resulting paththe path string to resolve against this path's parent other)Converts a given path string to a |
| public boolean | Returns: true if this path starts with the given path; otherwise
falsethe given path other)Tests if this path starts with the given path. |
| public default boolean | Returns: true if this path starts with the given path; otherwise
falsethe given path string other)Tests if this path starts with a |
| public Path | Returns: a newPath object that is a subsequence of the name
elements in this Paththe index of the first element, inclusive beginIndex, int the index of the last element, exclusive endIndex)Returns a relative |
| public Path | Returns: aPath object representing the absolute pathReturns a |
| public default File | |
| public Path | Returns: an absolute path represent the real path of the file located by this objectoptions indicating how symbolic links are handled options)Returns the real path of an existing file. |
| public String | Returns: the string representation of this pathReturns the string representation of this path. |
| public URI |
| compareTo | back to summary |
|---|---|
| public int compareTo(Path other) Redeclares java. Compares two abstract paths lexicographically. The ordering defined by this method is provider specific, and in the case of the default provider, platform specific. This method does not access the file system and neither file is required to exist. This method may not be used to compare paths that are associated with different file system providers.
| |
| endsWith | back to summary |
|---|---|
| public boolean endsWith(Path other) Tests if this path ends with the given path. If the given path has N elements, and no root component, and this path has N or more elements, then this path ends with the given path if the last N elements of each path, starting at the element farthest from the root, are equal. If the given path has a root component then this path ends with the given path if the root component of this path ends with the root component of the given path, and the corresponding elements of both paths are equal. Whether or not the root component of this path ends with the root component of the given path is file system specific. If this path does not have a root component and the given path has a root component then this path does not end with the given path. If the given path is associated with a different
| |
| endsWith | back to summary |
|---|---|
| public default boolean endsWith(String other) Tests if this path ends with a Implementation Specification The default implementation is equivalent for this path to: endsWith(getFileSystem().getPath(other));
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object other) Tests this path for equality with the given object. If the given object is not a Path, or is a Path associated with a
different Whether or not two path are equal depends on the file system
implementation. In some cases the paths are compared without regard
to case, and others are case sensitive. This method does not access the
file system and the file is not required to exist. Where required, the
This method satisfies the general contract of the | |
| getFileName | back to summary |
|---|---|
| public Path getFileName() Returns the name of the file or directory denoted by this path as a
| |
| getFileSystem | back to summary |
|---|---|
| public FileSystem getFileSystem() Returns the file system that created this object.
| |
| getName | back to summary |
|---|---|
| public Path getName(int index) Returns a name element of this path as a The
| |
| getNameCount | back to summary |
|---|---|
| public int getNameCount() Returns the number of name elements in the path.
| |
| getParent | back to summary |
|---|---|
| public Path getParent() Returns the parent path, or The parent of this path object consists of this path's root
component, if any, and each element in the path except for the
farthest from the root in the directory hierarchy. This method
does not access the file system; the path or its parent may not exist.
Furthermore, this method does not eliminate special names such as "."
and ".." that may be used in some implementations. On UNIX for example,
the parent of " If this path has more than one element, and no root component, then this method is equivalent to evaluating the expression: subpath(0, getNameCount()-1);
| |
| getRoot | back to summary |
|---|---|
| public Path getRoot() Returns the root component of this path as a
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Computes a hash code for this path. The hash code is based upon the components of the path, and
satisfies the general contract of the
| |
| isAbsolute | back to summary |
|---|---|
| public boolean isAbsolute() Tells whether or not this path is absolute. An absolute path is complete in that it doesn't need to be combined with other path information in order to locate a file.
| |
| iterator | back to summary |
|---|---|
| public default Iterator Implements java. Returns an iterator over the name elements of this path. The first element returned by the iterator represents the name
element that is closest to the root in the directory hierarchy, the
second element is the next closest, and so on. The last element returned
is the name of the file or directory denoted by this path. The Implementation Specification The default implementation returns an | |
| normalize | back to summary |
|---|---|
| public Path normalize() Returns a path that is this path with redundant name elements eliminated. The precise definition of this method is implementation dependent but
in general it derives from this path, a path that does not contain
redundant name elements. In many file systems, the " This method does not access the file system; the path may not locate
a file that exists. Eliminating "
| |
| of | back to summary |
|---|---|
| public static Path of(String first, String... more) Returns a The Note that while this method is very convenient, using it will imply
an assumed reference to the default Path dir = ... Path path = dir.resolve("file");
| |
| of | back to summary |
|---|---|
| public static Path of(URI uri) Returns a This method iterates over the In the case of the default provider, identified by the URI scheme
"file", the given URI has a non-empty path component, and undefined query
and fragment components. Whether the authority component may be present
is platform specific. The returned The default provider provides a similar round-trip guarantee
to the so long as the original Path, the URI, and the new Path are all created in (possibly different invocations of) the same
Java virtual machine. Whether other providers make any guarantees is
provider specific and therefore unspecified.
| |
| register | back to summary |
|---|---|
| public WatchKey register(WatchService watcher, WatchEvent. Redeclares java. Registers the file located by this path with a watch service. In this release, this path locates a directory that exists. The
directory is registered with the watch service so that entries in the
directory can be watched. The
The The set of events may include additional implementation specific
event that are not defined by the enum The Where a file is registered with a watch service by means of a symbolic link then it is implementation specific if the watch continues to depend on the existence of the symbolic link after it is registered.
| |
| register | back to summary |
|---|---|
| public default WatchKey register(WatchService watcher, WatchEvent. Implements java. Registers the file located by this path with a watch service. An invocation of this method behaves in exactly the same way as the invocation register(watcher, events, new WatchEvent.Modifier[0]); Usage Example: Suppose we wish to register a directory for entry create, delete, and modify events: Path dir = ... WatchService watcher = ... WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); Implementation Specification The default implementation is equivalent for this path to: register(watcher, events, new WatchEvent.Modifier[0]);
| |
| relativize | back to summary |
|---|---|
| public Path relativize(Path other) Constructs a relative path between this path and a given path. Relativization is the inverse of For any two p When symbolic links are supported, then whether the resulting path,
when resolved against this path, yields a path that can be used to locate
the
| |
| resolve | back to summary |
|---|---|
| public Path resolve(Path other) Resolve the given path against this path. If the
| |
| resolve | back to summary |
|---|---|
| public default Path resolve(String other) Converts a given path string to a Implementation Specification The default implementation is equivalent for this path to: resolve(getFileSystem().getPath(other));
| |
| resolve | back to summary |
|---|---|
| public default Path resolve(Path first, Path... more) Resolves a path against this path, and then iteratively resolves any additional paths. This method resolves Implementation Specification The default implementation is equivalent to the result obtained with: Path result = resolve(first); for (Path p : more) { result = result.resolve(p); }
| |
| resolve | back to summary |
|---|---|
| public default Path resolve(String first, String... more) Converts a path string to a path, resolves that path against this path, and then iteratively performs the same procedure for any additional path strings. This method converts Implementation Specification The default implementation is equivalent to the result obtained with: Path result = resolve(first); for (String s : more) { result = result.resolve(s); }
| |
| resolveSibling | back to summary |
|---|---|
| public default Path resolveSibling(Path other) Resolves the given path against this path's Implementation Specification The default implementation is equivalent for this path to: (getParent() == null) ? other : getParent().resolve(other); other == null, in which case a
NullPointerException is thrown.
| |
| resolveSibling | back to summary |
|---|---|
| public default Path resolveSibling(String other) Converts a given path string to a Implementation Specification The default implementation is equivalent for this path to: resolveSibling(getFileSystem().getPath(other));
| |
| startsWith | back to summary |
|---|---|
| public boolean startsWith(Path other) Tests if this path starts with the given path. This path starts with the given path if this path's root
component starts with the root component of the given path,
and this path starts with the same name elements as the given path.
If the given path has more name elements than this path then Whether or not the root component of this path starts with the root component of the given path is file system specific. If this path does not have a root component and the given path has a root component then this path does not start with the given path. If the given path is associated with a different
| |
| startsWith | back to summary |
|---|---|
| public default boolean startsWith(String other) Tests if this path starts with a Implementation Specification The default implementation is equivalent for this path to: startsWith(getFileSystem().getPath(other));
| |
| subpath | back to summary |
|---|---|
| public Path subpath(int beginIndex, int endIndex) Returns a relative The
| |
| toAbsolutePath | back to summary |
|---|---|
| public Path toAbsolutePath() Returns a If this path is already | |
| toFile | back to summary |
|---|---|
| public default File toFile() Returns a If this path was created by invoking the Implementation Specification The default implementation is equivalent for this path to: new File(toString()); FileSystem which created this Path is the default
file system; otherwise an UnsupportedOperationException is
thrown.
| |
| toRealPath | back to summary |
|---|---|
| public Path toRealPath(LinkOption... options) throws IOException Returns the real path of an existing file. The precise definition of this method is implementation dependent but
in general it derives from this path, an If this path is relative then its absolute path is first obtained,
as if by invoking the The
| |
| toString | back to summary |
|---|---|
| public String toString() Returns the string representation of this path. If this path was created by converting a path string using the
The returned path string uses the default name | |
| toUri | back to summary |
|---|---|
| public URI toUri() Returns a URI to represent this path. This method constructs an absolute In the case of the default provider, the URI is hierarchical with
a The default provider provides a similar round-trip guarantee
to the so long as the original Path, the URI, and the new Path are all created in (possibly different invocations of) the same
Java virtual machine. Whether other providers make any guarantees is
provider specific and therefore unspecified.
When a file system is constructed to access the contents of a file as a file system then it is highly implementation specific if the returned URI represents the given path in the file system or it represents a compound URI that encodes the URI of the enclosing file system. A format for compound URIs is not defined in this release; such a scheme may be added in a future release. | |