1Z0-829 Java I/O API and Securing Applications Practice Question
Which THREE are valid ways to read the contents of a text file into a String in Java?
⚠ Common exam trap
Many candidates confuse `Files.readAllLines()` (which returns a `List`) with `Files.readString()` (which returns a `String`), or assume `FileReader` has a `readString()` method when it does not.
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
✓
String content = Files.readString(Path.of("file.txt"));
`Files.readString()` (introduced in Java 11) reads the entire content of a text file into a `String` in one call, handling charset encoding (default UTF-8) and closing the file automatically. It is the simplest and most direct method for this task.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
String content = Files.readString(Path.of("file.txt"));
Why this is correct
Files.readString() reads entire file into a String.
- ✗
String content = new FileReader("file.txt").readString();
Why it's wrong here
FileReader does not have readString() method.
- ✗
String content = Files.readAllLines(Path.of("file.txt"));
Why it's wrong here
readAllLines returns List<String>, not String.
- ✓
String content = new Scanner(new File("file.txt")).useDelimiter("\\Z").next();
Why this is correct
Scanner with delimiter \\Z reads entire file content as one token.
- ✓
String content = Files.lines(Path.of("file.txt")).collect(Collectors.joining("\n"));
Why this is correct
Files.lines() returns a stream of lines, then joining combines them.
Go deeper
Related to this question
About these practice questions
One of 513 original 1Z0-829 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
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.