Path subpath() method in Java with Examples
The subpath(int beginIndex, int endIndex) method of java.nio.file.Path used to return a relative Path that is a subsequence of the name elements of this path. We will pass begin and end indexes to construct a subpath. The beginIndex and endIndex parameters specify the subsequence of name elements. The name element closest to the root in the directory hierarchy is index 0 and name element that is farthest from the root has index count-1. The returned subpath object has the name elements that begin at beginIndex and extend to the element at index endIndex-1.
Path subpath(int beginIndex, int endIndex)
Parameters: This method accepts a two parameters:
- beginIndex which is the index of the first element, inclusive and
- endIndex which is the index of the last element, exclusive.
Return value: This method returns a new Path object that is a subsequence of the name elements in this Path.
Exception: This method throws IllegalArgumentException if beginIndex is negative, or greater than or equal to the number of elements. If endIndex is less than or equal to beginIndex, or larger than the number of elements.
Below programs illustrate subpath() method: