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
〈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.
题解
程序第 13 行开始会保留路径为「txt」结尾的 Path 对象,接着读取剩下来的路径所指到的文件之所有的内容,并输出至标准输出串流中。paths 变量参考到的串流对象中的 Path 对象,只有「data.txt」会被 filter 保留,因此只会读取并输出「data.txt」的文件内容。
