1Z0-829 Java I/O API and Securing Applications Practice Question
A developer needs to read a large text file (several gigabytes) line by line as efficiently as possible, processing each line without loading the entire file into memory. Which approach should the developer use?
⚠ Common exam trap
It's easy for candidates to choose `BufferedReader` (Option A) because it is familiar, but they overlook that `Files.lines` is the modern, stream-based API that is both more idiomatic and safer for large files, and that `Files.readAllLines` (Option C) is a common pitfall for memory exhaustion.
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
✓
Use Files.lines with a try-with-resources block, and process each line using the stream.
`Files.lines` returns a `Stream<String>` that lazily reads lines from the file, processing them one at a time without loading the entire file into memory. The try-with-resources block ensures the underlying file handle is closed automatically, preventing resource leaks. This approach is specifically designed for efficient, memory-safe line-by-line processing of large files.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Use a FileReader wrapped in a BufferedReader, and process each line in a loop.
Why it's wrong here
Although it reads line by line, it requires manual handling of the resource closure to avoid leaks. The approach is correct but less concise than using Files.lines with try-with-resources.
- ✗
Use a FileInputStream to read bytes, then manually parse newline characters.
Why it's wrong here
FileInputStream reads raw bytes, requiring low-level parsing, which is more error-prone and less efficient for line-oriented processing.
- ✗
Use Files.readAllLines to read the entire file into a List<String> and then iterate.
Why it's wrong here
Files.readAllLines loads the entire file into memory, which would cause OutOfMemoryError for a large file.
- ✓
Use Files.lines with a try-with-resources block, and process each line using the stream.
Why this is correct
Files.lines returns a Stream that reads lines lazily, and try-with-resources ensures the underlying file handle is closed automatically. This is the most efficient and idiomatic solution.
Go deeper
Related to this question
About these practice questions
Courseiva writes every 1Z0-829 question from scratch — 513 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. 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.