Java Reference
In-Depth Information
Creating a Path Object
A FileSystem object acts as a factory to create a Path object. You can use the getPath() method of the FileSystem
class to create a Path object. The following snippet of code creates a Path object for file path C:\poems\luci1.txt on
Windows:
Path p1 = FileSystems.getDefault().getPath("C:\\poems\\luci1.txt");
You can pass components of a path separately to the getPath() method when constructing a Path object. Java
will take care of using an appropriate platform-dependent file name separators. The following statement creates a
Path object to represent the C:\poems\luci1.txt path on Windows:
Path p2 = FileSystems.getDefault().getPath("C:", "poems", "luci1.txt");
The Path API includes a utility class called Paths whose sole job is to create a Path object from the components
of a path string or a URI . The Paths.get() static method creates a Path object. Internally, it delegates the call to the
default FileSystem object. The following snippet of code creates Path objects to represent the same path,
C:\poems\luci1.txt :
Path p3 = Paths.get("C:\\poems\\luci1.txt");
Path p4 = Paths.get("C:", "poems", "luci1.txt");
You can create a Path object from an empty path such as Paths.get("") . a Path object with an empty path
refers to the default directory of the file system. a default directory is the same as the current working directory.
Tip
Accessing Components of a Path
A path in a file system consists of one or more components. The methods of the Path interface let you access those
components.
The getNameCount() method returns the number of components in a Path object excluding the root. For
example, the path C:\poems\luci1.txt consists of three components: a root of C: , and two components named poems
and luci1.txt . In this case, the getNameCount() method will return 2. The getName(int index) method returns the
component name at the specified index . The component that is closest to the root has an index of 0. The component
that is farthest from the root has an index of count - 1 . In the path C:\poems\luci1.txt , the poems component has
an index of 0 and the luci1.txt component has an index of 1.
The getParent() method returns the parent of a path. If a path does not have a parent, it returns null . The
parent of a path is the path itself without the farthest component from the root. For example, the parent of the path
C:\poems\luci.txt is C:\poems . The relative path test.txt has no parent.
The getRoot() method returns the root of the path. If a path does not have a root, it returns null . For example,
the path C:\poems\luci1.txt on Windows has C:\ as its root.
The getFileName() method returns the file name denoted by the path. If a path has no file name, it returns null .
The file name is the farthest component from the root. For example, in the path C:\poems\luci1.txt , luci1.txt is
the file name.
You can check if a path represents an absolute path by using the isAbsolute() method. Note that a path does not
have to exist in the file system to get information about its components. The Path API uses the information provided
in the path string to give you all these pieces of information.
 
 
Search WWH ::




Custom Search