Consider the following program:
import java.nio.file.*; class SubPath { public static void main(String []args) { Path aPath = Paths.get("D:\\OCPJP\\programs\\..\\NIO\src\\.\\SubPath.java"); aPath = aPath.normalize(); System.out.println(aPath.subpath(2, 3)); } }
This program prints the following:.
B.
the normalize()
method removes redundant name elements in the given path,
so after the call to the normalize()
method, the aPath
value is D:\OCPJP\NIO2\src\SubPath.java
.
the subpath
(int beginIndex
, int endIndex
) method returns a path based on the values of beginIndex
and endIndex
.
the name that is closest to the root has index 0; note that the root itself (in this case, D:\) is not considered an element in the path.
hence, the name elements OCPJP
, "NIO2", "src", and Subpath.java
are in index positions 0, 1, 2, and 3, respectively.
Note that beginIndex
is the index of the first element, inclusive of that element; endIndex
is the index of the last element, exclusive of that element.
hence, the subpath
is src, which is at index position 2 in this path.