Given the code fragment:
Path p1 = Paths.get("/Pics/MyPic.jpeg");
System.out.println(p1.getNameCount()
+ ":" + p1.getName(1)
+ ":" + p1.getFileName());
Assume that the Pics directory does NOT exist.
What is the result?
A. An exception is thrown at run time.
B.
2:MyPic.jpeg:MyPic.jpeg
C.
1:Pics:/Pics/MyPic.jpeg
D.
2:Pics:MyPic.jpeg
题解
Path 对象的 getNameCount 方法会计算路径结构中文件或是目录名称的数量,在这个题目中的路径为「/Pics/MyPic.jpeg」,因此有「Pics」和「MyPic.jpeg」两个名称。
Path 对象的 getName 方法会取得路径结构中的文件或是目录名称,索引值从 0 开始。
Path 对象的 getFileName 方法会取得最后一个名称。
因此这题不论文件或是目录存不存在,都会输出:
2:MyPic.jpeg:MyPic.jpeg
