Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 190

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?

    Correct Answer: C

    To print the content of the 'courses.txt' file, the appropriate method is to read all the lines of the file into a List using Files.readAllLines(file). This List can then be streamed and each line can be printed. The Files.readAllLines function returns a List of Strings, where each String is a line from the file, which can then be processed using stream(). The other options either incorrectly use Files.list, which is for listing files in a directory, or incorrectly handle Streams.

Discussion
Svetleto13Option: C

C,tested.

asdfjhfgjuaDCVOption: C

C is the correct

steefaandOption: C

C is correct.

duydnOption: C

C is correct.

NoraYuOption: C

option D would be right if use method lines(path) rather than list(path)

maslacOption: C

Correct answer is C static List<String> readAllLines(Path path) - Read all lines from a file.

jduarteOption: D

Option D, correct answer.

maslac

In what parallel universe is this the correct answer? static Stream<Path> list(Path dir) - Return a lazily populated Stream, the elements of which are the entries in the directory.