1Z0-829 Java I/O API and Securing Applications Practice Question
A company needs to read a large text file (over 2 GB) line by line in a Java application while minimizing memory footprint. Which approach is most efficient?
⚠ Common exam trap
A common mix-up: candidates choose `Scanner` (Option C) because it is familiar from simple file reading, but they overlook its higher memory overhead and lack of automatic resource management compared to the stream-based `Files.lines()` with try-with-resources.
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(path) with try-with-resources
`Files.lines(path)` returns a `Stream<String>` that lazily reads lines from the file, processing them one at a time without loading the entire file into memory. Combined with try-with-resources, the underlying `BufferedReader` is automatically closed, ensuring efficient resource management even for files over 2 GB.
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 Files.readAllLines(path)
Why it's wrong here
Reads entire file into memory, causing OOM for large files.
- ✗
Use FileOutputStream with read() loop
Why it's wrong here
Designed for binary output; not suitable for text line reading.
- ✗
Use Scanner with File and loop hasNextLine()
Why it's wrong here
Scanner is slower and less efficient than BufferedReader for line reading.
- ✓
Use Files.lines(path) with try-with-resources
Why this is correct
Lazy stream reading minimizes memory; auto-closes resource.
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.