Java SE 8 Programmer II Exam | Code Fragment to Print courses.txt File | Oracle

Print Content of courses.txt File

Question

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?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

D.

The correct answer is D. Stream<String> fc = Files.lines(file); fc.forEach(s -> System.out.println(s));

Explanation:

In the given code fragment, the Path object named "file" is created using the Paths.get() method which takes a string representing the path of the file. The file object is a reference to the "courses.txt" file.

To print the contents of the file, we need to read the lines from the file and print them. There are several methods available in the Files class that can be used to read the lines from the file, such as:

  • Files.readAllLines(Path path): This method reads all lines from the file as a List<String>.
  • Files.lines(Path path): This method returns a Stream<String> of lines from the file.
  • Files.newBufferedReader(Path path): This method returns a BufferedReader object that can be used to read the lines from the file.

Option A (List<String> fc = Files.list(file); fc.stream().forEach(s -> System.out.println(s));) is incorrect because the Files.list() method returns a Stream<Path> of the files and directories in the directory pointed to by the given path, not the lines of the file.

Option B (Stream<String> fc = Files.readAllLines(file); fc.forEach(s -> System.out.println(s));) is incorrect because the readAllLines() method returns a List<String> of all the lines in the file, not a Stream<String>.

Option C (List<String> fc = readAllLines(file); fc.stream().forEach(s -> System.out.println(s));) is incorrect because the readAllLines() method is not defined in the code, and it is not a static method of any class.

Option D (Stream<String> fc = Files.lines(file); fc.forEach(s -> System.out.println(s));) is correct because the lines() method returns a Stream<String> of lines from the file. We can then use the forEach() method to print each line in the stream.