Java Reference
In-Depth Information
Working with Paths
Typically, a file system stores objects (files, directories, symbolic links, etc.) in a hierarchical fashion. A file system uses
one or more root nodes that serve as the root of the hierarchy. An object in a file system has a path, which is typically
represented as a string, such as C:\home\test.txt on Windows, and /home/test.txt on UNIX-like operating
systems. A path string may contain multiple components separated by a special character called separator or
delimiter . For example, the path C:\home\test.txt consists of three components: C:\ as the root, home as a directory,
and test.txt as a file name. A backslash is a path separator on Windows. UNIX-like operating systems use a forward
slash ( / ) as the path separator. Note that path representation is platform-dependent.
A path can be absolute or relative. If a path starts with a root node, it is an absolute path. A relative path does not
start with a root node. No additional information is needed to locate an object referred in a file system by an absolute
path. Additional information is needed to locate an object referred in a file system by a relative path. For example, on
Windows, the path C:\home\test.txt is an absolute path because it starts with the root node C:\ , whereas the path
luci1.txt is a relative path. To locate the luci1.txt file, you need more information, such as the path of the directory
in which it exists.
A Path object is a programmatic representation of a path of an object in a file system such as a file, a directory,
and a symbolic link. A file system path is platform-dependent, so is a Path object.
Path is an interface in the java.nio.file package. When you work with a Path object, it is most likely that you
will also need to work with its two companion classes: Paths and Files . A path does not have to exist in a file system
to create a Path object to represent it in a Java program.
as a developer, you will be using Path objects most of the time when working with NIO.2 apI. the path apI meets
most of the file I/O-related needs of a developer. It has been designed to work with the old java.io.File apI. You can
get a Path object from a File object using the method toPath() of the File class. You can get a File object from a Path
object using the toFile() method of a Path object.
Tip
You can perform two kinds of operations on a Path object:
Path-related operations
The methods in the Path interface let you perform path-related operations on a Path object that may include
the following:
File I/O operations
Accessing the components of a path such as the file name, root name, etc.
.txt , comparing if
Comparing and testing paths. For example, checking if a path ends with
two paths are identical, checking if a path is absolute or relative, etc.
The Path interface does not include any methods to perform file I/O operations. You need to use the Files class
to perform the file I/O operations on a Path object. The Files class consists of all static methods. I will cover using
the Files class shortly.
Combining and resolving paths.
 
 
Search WWH ::




Custom Search