1Z0-829 Java I/O API and Securing Applications Practice Question
A developer needs to write text to a file with UTF-8 encoding. Which class should be used?
⚠ Common exam trap
Many candidates assume FileWriter or BufferedWriter automatically use UTF-8, but they actually rely on the platform's default charset, which may vary across operating systems, leading to encoding bugs in production.
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
✓
new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)
It explicitly constructs an OutputStreamWriter with a FileOutputStream and StandardCharsets.UTF_8, ensuring the text is written with UTF-8 encoding. This is the standard approach when you need precise control over the character encoding, as the OutputStreamWriter acts as a bridge from byte streams to character streams using the specified charset.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
BufferedWriter
Why it's wrong here
BufferedWriter does not provide encoding control; it wraps a Writer.
- ✓
new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)
Why this is correct
This allows explicit UTF-8 encoding for writing text.
- ✗
FileWriter
Why it's wrong here
FileWriter uses default encoding, cannot specify UTF-8.
- ✗
PrintWriter with default constructor
Why it's wrong here
PrintWriter with File parameter uses default encoding.
Go deeper
Related to this question
About these practice questions
This 1Z0-829 question is part of Courseiva's 513-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam 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.