Home » openjdk-7 » java.nio.file » [javadoc | source]
java.nio.file
public interface: Path [javadoc | source]

All Implemented Interfaces:
    Comparable, Watchable, Iterable

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 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 {@code Path} can represent a root, 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 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. {@code 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 {@code 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 endWith 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.

Accessing Files

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.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");
    BufferReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);

Interoperability

Paths associated with the default provider are generally interoperable with the java.io.File class. Paths created by other providers are unlikely to be interoperable with the abstract path names represented by {@code java.io.File}. The toPath method may be used to obtain a {@code Path} from the abstract path name represented by a {@code java.io.File} object. The resulting {@code Path} can be used to operate on the same file as the {@code java.io.File} object. In addition, the 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.

Method from java.nio.file.Path Summary:
compareTo,   endsWith,   endsWith,   equals,   getFileName,   getFileSystem,   getName,   getNameCount,   getParent,   getRoot,   hashCode,   isAbsolute,   iterator,   normalize,   register,   register,   relativize,   resolve,   resolve,   resolveSibling,   resolveSibling,   startsWith,   startsWith,   subpath,   toAbsolutePath,   toFile,   toRealPath,   toString,   toUri
Method from java.nio.file.Path Detail:
 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 {@code FileSystem} to this path then {@code false} is returned.

 public boolean endsWith(String other)
    Tests if this path ends with a {@code Path}, constructed by converting the given path string, in exactly the manner specified by the endsWith(Path) method. On UNIX for example, the path "{@code foo/bar}" ends with "{@code foo/bar}" and "{@code bar}". It does not end with "{@code r}" or "{@code /bar}". Note that trailing separators are not taken into account, and so invoking this method on the {@code Path}"{@code foo/bar}" with the {@code String} "{@code bar/}" returns {@code true}.
 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 {@code FileSystem}, then this method returns {@code false}.

    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 isSameFile method may be used to check if two paths locate the same file.

    This method satisfies the general contract of the Object.equals method.

 public Path getFileName()
    Returns the name of the file or directory denoted by this path as a {@code 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 {@code Path} object.

    The {@code 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 {@code 0}. The element that is farthest from the root has index count {@code -1}.

 public int getNameCount()
    Returns the number of name elements in the path.
 public Path getParent()
    Returns the parent path, or {@code 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 "{@code /a/b/c}" is "{@code /a/b}", and the parent of {@code "x/y/.}" is "{@code 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 one or more elements, and no root component, then this method is equivalent to evaluating the expression:

    subpath(0, getNameCount()-1);
    
 public Path getRoot()
    Returns the root component of this path as a {@code Path} object, or {@code null} if this path does not have a root component.
 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 Object.hashCode method.

 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 "{@code .}" and "{@code ..}" are special names used to indicate the current directory and parent directory. In such file systems all occurrences of "{@code .}" are considered redundant. If a "{@code ..}" is preceded by a non-"{@code ..}" name then both names are considered redundant (the process to identify such names is repeated until is it no longer applicable).

    This method does not access the file system; the path may not locate a file that exists. Eliminating "{@code ..}" 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.

 public WatchKey register(WatchService watcher,
    Kind<?> events) throws IOException
    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

        watchable. 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);
    
 public WatchKey register(WatchService watcher,
    Kind<?>[] events,
    Modifier modifiers) throws IOException
    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 {@code events} parameter is the events to register and may contain the following events:

    The context for these events is the relative path between the directory located by this path, and the path that locates the directory entry that is created, deleted, or modified.

    The set of events may include additional implementation specific event that are not defined by the enum StandardWatchEventKinds

    The {@code modifiers} parameter specifies modifiers that qualify how the directory is registered. This release does not define any standard modifiers. It may contain implementation specific modifiers.

    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.

 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 {@code "/a/b"} and the given path is {@code "/a/b/c/d"} then the resulting relative path would be {@code "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 {@code other} is implementation dependent. For example, if this path is {@code "/a/b"} and the given path is {@code "/a/x"} then the resulting relative path may be {@code "../x"}. If {@code "b"} is a symbolic link then is implementation dependent if {@code "a/b/../x"} would locate the same file as {@code "/a/x"}.

 public Path resolve(Path other)
    Resolve the given path against this path.

    If the {@code other} parameter is an absolute path then this method trivially returns {@code other}. If {@code 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.

 public Path resolve(String other)
    Converts a given path string to a {@code Path} and resolves it against this {@code Path} in exactly the manner specified by the resolve method. For example, suppose that the name separator is "{@code /}" and a path represents "{@code foo/bar}", then invoking this method with the path string "{@code gus}" will result in the {@code Path} "{@code 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 "{@code /}" and a path represents "{@code dir1/dir2/foo}", then invoking this method with the {@code Path} "{@code bar}" will result in the {@code Path} "{@code dir1/dir2/bar}". If this path does not have a parent path, or {@code other} is absolute , then this method returns {@code other}. If {@code 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 {@code 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 {@code 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 {@code FileSystem} to this path then {@code false} is returned.

 public boolean startsWith(String other)
    Tests if this path starts with a {@code Path}, constructed by converting the given path string, in exactly the manner specified by the startsWith(Path) method. On UNIX for example, the path "{@code foo/bar}" starts with "{@code foo}" and "{@code foo/bar}". It does not start with "{@code f}" or "{@code fo}".
 public Path subpath(int beginIndex,
    int endIndex)
    Returns a relative {@code Path} that is a subsequence of the name elements of this path.

    The {@code beginIndex} and {@code endIndex} parameters specify the subsequence of name elements. The name that is closest to the root in the directory hierarchy has index {@code 0}. The name that is farthest from the root has index count {@code -1}. The returned {@code Path} object has the name elements that begin at {@code beginIndex} and extend to the element at index {@code endIndex-1}.

 public Path toAbsolutePath()
    Returns a {@code 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 {@code Path} is associated with the default provider, then this method is equivalent to returning a {@code File} object constructed with the {@code String} representation of this path.

    If this path was created by invoking the {@code File} toPath method then there is no guarantee that the {@code File} object returned by this method is equal to the original {@code File}.

 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 absolute path that locates the same file as this path, but with name elements that represent the actual name of the directories and the file. For example, where filename comparisons on a file system are case insensitive then the name elements represent the names in their actual case. Additionally, the resulting path has redundant name elements removed.

    If this path is relative then its absolute path is first obtained, as if by invoking the toAbsolutePath method.

    The {@code options} array may be used to indicate how symbolic links are handled. By default, symbolic links are resolved to their final target. If the option NOFOLLOW_LINKS is present then this method does not resolve symbolic links. Some implementations allow special names such as "{@code ..}" to refer to the parent directory. When deriving the real path, and a "{@code ..}" (or equivalent) is preceded by a non-"{@code ..}" name then an implementation will typically cause both names to be removed. When not resolving symbolic links and the preceding name is a symbolic link then the names are only removed if it guaranteed that the resulting path will locate the same file as this path.

 public String toString()
    Returns the string representation of this path.

    If this path was created by converting a path string using the getPath method then the path string returned by this method may differ from the original String used to create the path.

    The returned path string uses the default name separator to separate names in the path.

 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 {@code 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 {@code URI} will end with a slash.

    The default provider provides a similar round-trip guarantee to the java.io.File class. For a given {@code Path} p it is guaranteed that

    Paths.get (p.toUri()).equals(p .toAbsolutePath ())
    so long as the original {@code Path}, the {@code URI}, and the new {@code 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.