Courseiva
Question 501 of 513
Java I/O API and Securing ApplicationseasyMultiple ChoiceObjective-mapped

1Z0-829 Java I/O API and Securing Applications Practice Question

A developer needs to read all lines from a text file named "data.txt" that uses UTF-8 encoding. Which code correctly reads the file using the NIO.2 API?

⚠ Common exam trap

Many candidates confuse `Files.readString()` (which returns a single string) with `Files.readAllLines()` (which returns a list of lines), or they mistakenly think `Files.readAllBytes().toString()` will convert bytes to text, when in fact it returns the object reference string.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Files.readAllLines(Path.of("data.txt"), StandardCharsets.UTF_8)

`Files.readAllLines()` reads all lines from a file into a `List<String>`, and it accepts a `Path` and a `Charset` (here `StandardCharsets.UTF_8`) to handle encoding. This method is part of the NIO.2 API and directly fulfills the requirement to read all lines from a UTF-8 encoded file.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Files.lines(Path.of("data.txt"), StandardCharsets.UTF_8).collect(Collectors.toList())

    Why it's wrong here

    This reads lines into a Stream and collects to a list, but it's less efficient and not the direct method for reading all lines.

  • Files.readString(Path.of("data.txt"), StandardCharsets.UTF_8)

    Why it's wrong here

    Files.readString reads the entire file into a single String, not a list of lines.

  • Files.readAllBytes(Path.of("data.txt")).toString()

    Why it's wrong here

    Files.readAllBytes returns a byte array, and calling toString() does not decode lines properly.

  • Files.readAllLines(Path.of("data.txt"), StandardCharsets.UTF_8)

    Why this is correct

    Files.readAllLines directly returns a List<String> with the specified charset, which is the simplest correct approach.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Last reviewed: Jun 25, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.