An object that may be used to locate a file in a file system. It will typically represent a system dependent file path.
A {@code Path} represents a path that is hierarchical and composed of asequence 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 {@code Path} can represent aroot, a root and a sequence of names, or simply one or more name elements. A {@code Path} is considered to be an empty path if it consistssolely 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. {@code Path} defines the {@link #getFileName() getFileName}, {@link #getParent getParent}, {@link #getRoot getRoot}, and {@link #subpath subpath} methods to access the path components or a subsequence of its nameelements.
In addition to accessing the components of a path, a {@code Path} alsodefines the {@link #resolve(Path) resolve} and {@link #resolveSibling(Path) resolveSibling} methods to combine paths. The {@link #relativize relativize}method that can be used to construct a relative path between two paths. Paths can be {@link #compareTo compared}, and tested against each other using the {@link #startsWith startsWith} and {@link #endsWith endsWith} methods.
This interface extends {@link Watchable} interface so that a directorylocated by a path can be {@link #register registered} with a {@link 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.
Accessing Files
Paths may be used with the {@link Files} class to operate on files,directories, and other types of files. For example, suppose we want a {@link java.io.BufferedReader} to read text from a file "{@code access.log}". The file is located in a directory " {@code 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);
Interoperability
Paths associated with the default {@link java.nio.file.spi.FileSystemProvider provider} are generally interoperablewith the {@link java.io.File java.io.File} class. Paths created by otherproviders are unlikely to be interoperable with the abstract path names represented by {@code java.io.File}. The {@link java.io.File#toPath toPath}method may be used to obtain a {@code Path} from the abstract path namerepresented by a {@code java.io.File} object. The resulting {@code Path} canbe used to operate on the same file as the {@code java.io.File} object. Inaddition, the {@link #toFile toFile} method is useful to construct a {@code File} from the {@code String} representation of a {@code Path}.
Concurrency
Implementations of this interface are immutable and safe for use by multiple concurrent threads.
@since 1.7
@see Paths