Given the code fragment:
Path file = Paths.get("courses.txt");
// line n1
Assume the courses.txt is accessible.
Which code fragment can be inserted at line n1 to enable the code to print the content of the courses.txt file?
A.
List<String> fc = Files.list(file);
fc.stream().forEach (s -> System.out.println(s));
B.
Stream<String> fc = Files.readAllLines(file);
fc.forEach (s -> System.out.println(s));
C.
List<String> fc = readAllLines(file);
fc.stream().forEach (s -> System.out.println(s));
D.
Stream<String> fc = Files.lines(file);
fc.forEach (s -> System.out.println(s));
題解
選項A,Files類別的list方法會回傳Stream
選項B,Files類別的readAllLines方法會回傳List
選項C,readAllLines方法前應該指定Files類別。
選項D,正確答案。