Courseiva
Java I/O API and Securing ApplicationsmediumMultiple ChoiceObjective-mapped

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

A web server application writes access logs to a file. To ensure that log entries are written to disk immediately even if the JVM crashes, which approach is most appropriate?

⚠ Common exam trap

Candidates often confuse 'flush()' with 'sync()' and assume that flushing a buffered stream (like BufferedWriter or PrintWriter) guarantees disk persistence, when in fact it only pushes data to the next layer (e.g., OS buffer) and does not ensure an immediate disk write.

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 FileOutputStream and call flush() after each write.

FileOutputStream provides direct, unbuffered writes to the underlying file descriptor. While calling flush() does not force an immediate disk write (that requires sync() or FileChannel.force()), the unbuffered nature means data is passed to the operating system without intermediate caching in the JVM. Among the given options, this minimizes data loss risk because there is no JVM-level buffer that could hold unwritten data. Options B and C use buffering, which can delay writes, and D also uses an internal buffer. Note: For true guaranteed synchronous disk writes, additional system calls like FileDescriptor.sync() or FileChannel.force() are required.

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 FileOutputStream and call flush() after each write.

    Why this is correct

    Flushing after each write forces the data to be written to the underlying file system.

  • Use BufferedWriter and call write() then flush() periodically.

    Why it's wrong here

    BufferedWriter may still buffer if not flushed after each write; periodic flush is not immediate.

  • Use PrintWriter with autoFlush enabled.

    Why it's wrong here

    autoFlush only flushes on newline, not on every write.

  • Use FileWriter without any buffering.

    Why it's wrong here

    FileWriter may be buffered by the underlying system; flush still needed.

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 →

How Courseiva writes practice questions · Editorial policy

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.