Java Path Class

In this section we will discuss all the aspect of Path class of Java.

In Java, Path class is added from the SE 7 release. Java 7 has added the
java.nio.file package (new file IO). Prior to JDK7/JRE7 many of the methods
doesn't throw the exceptions on the encountered of an error and a developer
could not understand that what happened, so a new package is introduced in Java
7 for the file system to let the developer understand, what is happening on the
encounter of an error. The Path represents a path in the file system object of
which contains the name of a file, directory list to create path, check path,
locate path and manipulate files. The Path is system dependent, i.e. path of
file system of one Operating System is different from the file system of another
Operating System.

Name elements of Path are stored in a sequence i.e. the top level element of
directory structure is situated at the index 0 (zero) and the lowest level
element of directory structure is situated at the index (n-1), 'n' is the number
of name elements. There are various of methods are available in Path which are
used to get the information of the existing file. These are as follows :

Method Name

Description

getFileName()

This method is used to get the name of file or directory of the current
path.
Syntax : Path getFileName()

getName(int index)

This method is used to get the name of the specified index.
Syntax : Path getName(int index)

getNameCount()

This method is used to get the number of element in the specified path.
Syntax : int getNameCount()

getParent()

This method is used to get the parent directory.
Syntax : Path getParent()

getRoot()

This method is used to get the root of the directory.
Syntax : Path getRoot()

Except these above methods there are some more useful methods in Path.

Method Name

Description

endsWith(Path other)

This method tests whether the current path is ends with the
specified path or not.Syntax : boolean endsWith(Path other)

endsWith(String other)

This method tests whether the current path is ends with the specified
path made up with the specified string path name or not.Syntax :boolean endsWith(String other)

equals(Object other)

This method compares the current path with the given object that whether
they are equal or not.Syntax : boolean equals(Object other)

isAbsolute()

This method specifies that whether the current path is absolute or not.Syntax : boolean isAbsolute()

iterator()

This method gives an iterator to iterate over the name element of the
current path.Syntax : Iterator iterator()

resolve(Path other)

This method is used to join the current path with given path.Syntax : Path resolve(Path other)

resolve(String other)

This method converted the specified string to a path and then joins this
path with the current path.Syntax : Path resolve(String other)

startsWith(Path other)

This method tests whether the current path is started with the specified
path or not.Syntax : boolean startsWith(Path other)

startsWith(String other)

This method tests whether the current path is started with the specified
path made up with the specifie string path or not.Syntax : boolean startsWith(String other)

subpath(int beginIndex, int endIndex)

This method is used to get the relative path from the current path, the
returned path is the subsequence of the current path returned from the
specified index.
Syntax : Path subpath(int beginIndex, int endIndex)

toString()

This method is used to get the path as a String.Syntax : String toString()

toFile()

This method is used to get the File object from the current path.Syntax : File toFile()