Given the code fragment:
Stream<Path> files = Files.walk(Paths.get(System.getProperty("user.home")));
files.forEach(fName -> { //line n1
try {
Path aPath = fName.toAbsolutePath(); //line n2
System.out.println(fName + ":"
+ Files.readAttributes(aPath, BasicFileAttributes.class).creationTime());
} catch (IOException ex) {
ex.printStackTrace();
}
});
What is the result?
A. All files and directories under the home directory are listed along with their attributes.
B. A compilation error occurs at line n1.
C. The files in the home directory are listed along with their attributes.
D. A compilation error occurs at line n2.
题解
「System.getProperty("user.home")」会取得用户的家目录路径。Paths 类别的 get 方法可以传入路径字符串对象或是 URI 对象来产生 Path 对象。Files 类别的 walk 方法可以产生串流对象,以深度优先走访传入的 Path 对象所代表的文件目录。
所以选项 A 是正确答案。
