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().endWith(“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.
A
need help to understand how it results into answer A.
A is correct.
1) paths.filter(s -> s.toString().endsWith(“txt”)) reduce our stream just to one value.
2) forEach is a terminal operation, so stream is proccesed.
3) Files.readAllLines – read all lines from a file.
B.
The program prints:
Exception
It’s likely that you’ve messed smth up at your machine. E.g. the files do not exist or similar.
Assuming, that all the three files exists and accessible, the contents of the txt one would be printed by this code sample.
thank you for your guidance
A.
is correct answer. prints contents of .txt file.