Given the code fragment:
Path file = Paths.get ("courses.txt");
// line n1
courses.txt is accessible.
Assume the -
line n1 to enable the code to print the content of the courses.txt file?
Which code fragment can be inserted at
Given the code fragment:
Path file = Paths.get ("courses.txt");
// line n1
courses.txt is accessible.
Assume the -
line n1 to enable the code to print the content of the courses.txt file?
Which code fragment can be inserted at
To print the content of the courses.txt file, you can use the Files.lines method which returns a Stream of the lines from the file. Therefore, using Stream<String> fc = Files.lines(file); will be appropriate. This method directly correlates to a Stream, which matches the need of potentially performing operations like forEach for printing to the console. The other options are either syntactically incorrect or misuse the method types (like Files.readAllLines returning a List instead of a Stream).
Files.readAllLines returns a list of strings. the correct answer is C
D is correct with this addition : fc.forEach(n -> System.out.println(n));
B is incorrect because Files.readAllLines(file) return a list of strings not a Stream
C is correct since readAllLines returns List rather than Stream. For this to be true assume that Files is static imported and there is no need for it to be present in front of readAllLines.
Files.readAllLines returns a list of strings. The correct answer is C
Answer is B. Option D is incorrect because it can't print courses.txt, must add fc.forEach(System.out::print).
I mean answer is C.
The correct answer is D
C can be the answer with below modification. Stream<String> fc = Files.readAllLines (file).stream();