| Constructor and description |
|---|
WritablePath(Path delegate)Creates a writable wrapper that uses the platform default charset when reading the path contents. |
WritablePath(Path delegate, String encoding)Creates a writable wrapper for the supplied path. |
| Type Params | Return Type | Name and description |
|---|---|---|
|
public int |
compareTo(Path other)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.
|
|
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
|
|
public boolean |
endsWith(String other)Tests if this path ends with a Path, constructed by converting
the given path string, in exactly the manner specified by the endsWith(Path) endsWith(Path) method. On UNIX for example, the path
"foo/bar" ends with "foo/bar" and "bar". It does
not end with "r" or "/bar". Note that trailing separators
are not taken into account, and so invoking this method on the Path"foo/bar" with the String "bar/" returns
true.
|
|
public boolean |
equals(Object other)Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.
|
|
public Path |
getFileName()Returns the name of the file or directory denoted by this path as a Path object. The file name is the farthest element from
the root in the directory hierarchy.
|
|
public FileSystem |
getFileSystem()Returns the file system that created this object.
|
|
public Path |
getName(int index)Returns a name element of this path as a Path object.
The
|
|
public int |
getNameCount()Returns the number of name elements in the path.
|
|
public Path |
getParent()Returns the parent path, or null if this path does not
have a parent.
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:
|
|
public Path |
getRoot()Returns the root component of this path as a Path object,
or null if this path does not have a root component.
|
|
public int |
hashCode()Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap.
The general contract of
|
|
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.
|
|
public Iterator<Path> |
iterator()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 root component, if present, is not returned by the iterator.
|
|
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 "
|
|
public WatchKey |
register(WatchService watcher, WatchEvent.Kind<?>[] events, Modifier modifiers) |
|
public WatchKey |
register(WatchService watcher, WatchEvent.Kind<?> events) |
|
public Path |
relativize(Path other)Constructs a relative path between this path and a given path. Relativization is the inverse of resolution.
This method attempts to construct a relative path
that when resolved against this path, yields a
path that locates the same file as the given path. For example, on UNIX,
if this path is For any two normalized paths p and q, where q does not have a root component, 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 same file as
|
|
public Path |
resolve(Path other)Resolve the given path against this path. If the
|
|
public Path |
resolve(String other)Converts a given path string to a Path and resolves it against
this Path in exactly the manner specified by the resolve method. For example, suppose that the name
separator is "/" and a path represents "foo/bar", then
invoking this method with the path string "gus" will result in
the Path "foo/bar/gus".
|
|
public Path |
resolveSibling(Path other)Resolves the given path against this path's parent path. This is useful where a file name needs to be replaced with another file name. For example, suppose that the name separator is " /" and a path represents "dir1/dir2/foo", then invoking
this method with the Path "bar" will result in the Path "dir1/dir2/bar". If this path does not have a parent path,
or other is absolute, then this method
returns other. If other is an empty path then this method
returns this path's parent, or where this path doesn't have a parent, the
empty path.
|
|
public Path |
resolveSibling(String other)Converts a given path string to a Path and resolves it against
this path's parent path in exactly the manner
specified by the resolveSibling method.
|
|
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
|
|
public boolean |
startsWith(String other)Tests if this path starts with a Path, constructed by converting
the given path string, in exactly the manner specified by the startsWith(Path) startsWith(Path) method. On UNIX for example, the path
"foo/bar" starts with "foo" and "foo/bar". It
does not start with "f" or "fo".
|
|
public Path |
subpath(int beginIndex, int endIndex)Returns a relative Path that is a subsequence of the name
elements of this path.
The
|
|
public Path |
toAbsolutePath()Returns a Path object representing the absolute path of this
path.
If this path is already absolute then this method simply returns this path. Otherwise, this method resolves the path in an implementation dependent manner, typically by resolving the path against a file system default directory. Depending on the implementation, this method may throw an I/O error if the file system is not accessible.
|
|
public File |
toFile()Returns a File object representing this path. Where this Path is associated with the default provider, then this method is
equivalent to returning a File object constructed with the
String representation of this path.
If this path was created by invoking the
|
|
public Path |
toRealPath(LinkOption options) |
|
public String |
toString()Returns a string representation of the object.
|
|
public URI |
toUri()Returns a URI to represent this path. This method constructs an absolute URI with a scheme equal to the URI scheme that identifies the provider. The exact form of the scheme specific part is highly provider dependent. In the case of the default provider, the URI is hierarchical with
a path component that is absolute. The query and
fragment components are undefined. Whether the authority component is
defined or not is implementation dependent. There is no guarantee that
the The default provider provides a similar round-trip guarantee
to the File class. For a given Path.ofso 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.
|
|
public Writer |
writeTo(Writer out)Writes the wrapped path contents to the supplied writer. |
Creates a writable wrapper that uses the platform default charset when reading the path contents.
delegate - the path to wrapCompares 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.
other - the path compared to this path.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 FileSystem
to this path then false is returned.
other
- the given pathtrue if this path ends with the given path; otherwise
false Tests if this path ends with a Path, constructed by converting
the given path string, in exactly the manner specified by the endsWith(Path) endsWith(Path) method. On UNIX for example, the path
"foo/bar" ends with "foo/bar" and "bar". It does
not end with "r" or "/bar". Note that trailing separators
are not taken into account, and so invoking this method on the Path"foo/bar" with the String "bar/" returns
true.
endsWith(getFileSystem().getPath(other));
other
- the given path stringtrue if this path ends with the given path; otherwise
falseIndicates whether some other object is "equal to" this one.
The equals method implements an equivalence relation
on non-null object references:
x, x.equals(x) should return
true.
x and y, x.equals(y)
should return true if and only if
y.equals(x) returns true.
x, y, and z, if
x.equals(y) returns true and
y.equals(z) returns true, then
x.equals(z) should return true.
x and y, multiple invocations of
x.equals(y) consistently return true
or consistently return false, provided no
information used in equals comparisons on the
objects is modified.
x,
x.equals(null) should return false.
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.
equals method for class Object implements
the most discriminating possible equivalence relation on objects;
that is, for any non-null reference values x and
y, this method returns true if and only
if x and y refer to the same object
(x == y has the value true).
In other words, under the reference equality equivalence
relation, each equivalence class only has a single element.hashCode method, which states
that equal objects must have equal hash codes.obj - the reference object with which to compare.true if this object is the same as the obj
argument; false otherwise. Returns the name of the file or directory denoted by this path as a
Path object. The file name is the farthest element from
the root in the directory hierarchy.
null if this path has zero elementsReturns the file system that created this object.
Returns a name element of this path as a Path object.
The index parameter is the index of the name element to return.
The element that is closest to the root in the directory hierarchy
has index 0. The element that is farthest from the root
has index count-1.
index is negative, index is greater than or
equal to the number of elements, or this path has zero name
elementsindex
- the index of the elementReturns the number of name elements in the path.
0 if this path
only represents a root component Returns the parent path, or null if this path does not
have a parent.
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 "/a/b/c" is "/a/b", and the parent of
"x/y/." is "x/y". This method may be used with the normalize method, to eliminate redundant names, for cases where
shell-like navigation is required.
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);
Returns the root component of this path as a Path object,
or null if this path does not have a root component.
nullReturns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap.
The general contract of hashCode is:
hashCode method
must consistently return the same integer, provided no information
used in equals comparisons on the object is modified.
This integer need not remain consistent from one execution of an
application to another execution of the same application.
hashCode method on each of the two objects must produce the
same integer result.
hashCode method on each of the two objects
must produce distinct integer results. However, the programmer
should be aware that producing distinct integer results for
unequal objects may improve the performance of hash tables.
hashCode method defined
by class Object returns distinct integers for distinct objects.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.
true if, and only if, this path is absoluteReturns 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 root component, if present, is not returned by the iterator.
Iterator<Path> which, for
this path, traverses the Paths returned by
getName(index), where index ranges from zero to
getNameCount() - 1, inclusive.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 "."
and ".." are special names used to indicate the current directory
and parent directory. In such file systems all occurrences of "."
are considered redundant. If a ".." is preceded by a
non-".." name then both names are considered redundant (the
process to identify such names is repeated until it is no longer
applicable).
This method does not access the file system; the path may not locate
a file that exists. Eliminating ".." and a preceding name from a
path may result in the path that locates a different file than the original
path. This can arise when the preceding name is a symbolic link.
Constructs a relative path between this path and a given path.
Relativization is the inverse of resolution.
This method attempts to construct a relative path
that when resolved against this path, yields a
path that locates the same file as the given path. For example, on UNIX,
if this path is "/a/b" and the given path is "/a/b/c/d"
then the resulting relative path would be "c/d". Where this
path and the given path do not have a root component,
then a relative path can be constructed. A relative path cannot be
constructed if only one of the paths have a root component. Where both
paths have a root component then it is implementation dependent if a
relative path can be constructed. If this path and the given path are
equal then an empty path is returned.
For any two normalized paths p and q, where q does not have a root component,
p.relativize(p.resolve(q)).equals(q)
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 same file as other is implementation
dependent. For example, if this path is "/a/b" and the given
path is "/a/x" then the resulting relative path may be "../x". If "b" is a symbolic link then is implementation
dependent if "a/b/../x" would locate the same file as "/a/x".
other is not a Path that can be relativized
against this pathother
- the path to relativize against this pathResolve the given path against this path.
If the other parameter is an absolute
path then this method trivially returns other. If other
is an empty path then this method trivially returns this path.
Otherwise this method considers this path to be a directory and resolves
the given path against this path. In the simplest case, the given path
does not have a root component, in which case this method
joins the given path to this path and returns a resulting path
that ends with the given path. Where the given path has
a root component then resolution is highly implementation dependent and
therefore unspecified.
other
- the path to resolve against this path Converts a given path string to a Path and resolves it against
this Path in exactly the manner specified by the resolve method. For example, suppose that the name
separator is "/" and a path represents "foo/bar", then
invoking this method with the path string "gus" will result in
the Path "foo/bar/gus".
resolve(getFileSystem().getPath(other));
other
- the path string to resolve against this path Resolves the given path against this path's parent
path. This is useful where a file name needs to be replaced with
another file name. For example, suppose that the name separator is
"/" and a path represents "dir1/dir2/foo", then invoking
this method with the Path "bar" will result in the Path "dir1/dir2/bar". If this path does not have a parent path,
or other is absolute, then this method
returns other. If other is an empty path then this method
returns this path's parent, or where this path doesn't have a parent, the
empty path.
(getParent() == null) ? other : getParent().resolve(other);
unless other == null, in which case a
NullPointerException is thrown.other
- the path to resolve against this path's parent Converts a given path string to a Path and resolves it against
this path's parent path in exactly the manner
specified by the resolveSibling method.
resolveSibling(getFileSystem().getPath(other));
other
- the path string to resolve against this path's parentTests 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 false
is returned.
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 FileSystem
to this path then false is returned.
other
- the given pathtrue if this path starts with the given path; otherwise
false Tests if this path starts with a Path, constructed by converting
the given path string, in exactly the manner specified by the startsWith(Path) startsWith(Path) method. On UNIX for example, the path
"foo/bar" starts with "foo" and "foo/bar". It
does not start with "f" or "fo".
startsWith(getFileSystem().getPath(other));
other
- the given path stringtrue if this path starts with the given path; otherwise
false Returns a relative Path that is a subsequence of the name
elements of this path.
The beginIndex and endIndex parameters specify the
subsequence of name elements. The name that is closest to the root
in the directory hierarchy has index 0. The name that is
farthest from the root has index count-1. The returned Path object has the name elements
that begin at beginIndex and extend to the element at index endIndex-1.
beginIndex is negative, or greater than or equal to
the number of elements. If endIndex is less than or
equal to beginIndex, or larger than the number of elements.beginIndex
- the index of the first element, inclusiveendIndex
- the index of the last element, exclusivePath object that is a subsequence of the name
elements in this Path Returns a Path object representing the absolute path of this
path.
If this path is already absolute then this method simply returns this path. Otherwise, this method resolves the path in an implementation dependent manner, typically by resolving the path against a file system default directory. Depending on the implementation, this method may throw an I/O error if the file system is not accessible.
user.dirPath object representing the absolute path Returns a File object representing this path. Where this Path is associated with the default provider, then this method is
equivalent to returning a File object constructed with the
String representation of this path.
If this path was created by invoking the File toPath method then there is no guarantee that the File object returned by this method is equal to the
original File.
Path is not associated with the default providernew File(toString());
if the FileSystem which created this Path is the default
file system; otherwise an UnsupportedOperationException is
thrown.File object representing this path
Returns a string representation of the object.
toString method returns a string that
"textually represents" this object. The result should
be a concise but informative representation that is easy for a
person to read.
It is recommended that all subclasses override this method.
The string output is not necessarily stable over time or across
JVM invocations.toString method for class Object
returns a string consisting of the name of the class of which the
object is an instance, the at-sign character `@', and
the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
Returns a URI to represent this path.
This method constructs an absolute URI with a scheme equal to the URI scheme that identifies the provider. The exact form of the scheme specific part is highly provider dependent.
In the case of the default provider, the URI is hierarchical with
a path component that is absolute. The query and
fragment components are undefined. Whether the authority component is
defined or not is implementation dependent. There is no guarantee that
the URI may be used to construct a java.io.File.
In particular, if this path represents a Universal Naming Convention (UNC)
path, then the UNC server name may be encoded in the authority component
of the resulting URI. In the case of the default provider, and the file
exists, and it can be determined that the file is a directory, then the
resulting URI will end with a slash.
The default provider provides a similar round-trip guarantee
to the File class. For a given Path p it
is guaranteed that
Path.ofso long as the original(p.toUri()).equals(p.toAbsolutePath())
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.
Copyright © 2003-2026 The Apache Software Foundation. All rights reserved.