Consider the following program and predict the output:
import java.nio.file.*; class Main {/* ww w.j ava 2 s . c om*/ public static void main(String[] args) { // assume that the current directory is "D:\workspace\ch14-test" Path testFilePath = Paths.get(".\\Main"); System.out.println("file name:" + testFilePath.getFileName()); System.out.println("absolute path:" + testFilePath.toAbsolutePath()); System.out.println("Normalized path:" + testFilePath.normalize()); } }
a)file name:Main// w ww . j av a 2s .c o m absolute path:D:\workspace\ch14-test\.\Main Normalized path:Main b)file name:Main absolute path:D:\workspace\ch14-test\Main Normalized path:Main c)file name:Main absolute path:D:\workspace\ch14-test\.\Main Normalized path:D:\workspace\ch14-test\.\Main d)file name:Main absolute path:D:\workspace\ch14-test\.\Main Normalized path:D:\workspace\ch14-test\Main
a)
The absolute path adds the path from the root directory;
however, it does not normalize the path.
Hence, " .\" will be retained in the resultant path. On the other hand, the normalize()
method normalizes the path but does not make it absolute.