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 是正確答案。
