The data.doc, data.txt and data.xml files are accessible and contain text.



Given the code fragment:

Stream<Path> paths = Stream.of(Paths.get("data.doc"),
        Paths.get("data.txt"),
        Paths.get("data.xml"));
paths.filter(s -> s.toString().endsWith("txt")).forEach(
        s -> {
            try {
                Files.readAllLines(s)
                .stream()
                .forEach(System.out::println); //line n1
            } catch (IOException e) {
                System.out.println("Exception");
            }
        });

What is the result?

A. The program prints the content of data.txt file.
B. The program prints:

Exception
〈The content of the data.txt file〉
Exception

C. A compilation error occurs at line n1.
D. The program prints the content of the three files.

題解

程式第24行開始會保留路徑為「txt」結尾的Path物件,接著讀取剩下來的路徑所指到的檔案之所有的內容,並輸出至標準輸出串流中。paths變數參考到的串流物件中的Path物件,只有「data.txt」會被filter保留,因此只會讀取並輸出「data.txt」的檔案內容。