What is the correct way to close a file in Java after writing to it?

2024

What is the correct way to close a file in Java after writing to it?

  1. A.

    fileInput.close();

  2. B.

    file.close();

  3. C.

    fileWriter.close();

  4. D.

    fileStream.close();

  5. E.

    Question not attempted

Attempted by 85 students.

Show answer & explanation

Correct answer: C

When a FileWriter object is used for writing, calling fileWriter.close() closes the writer and releases the file resource.

Explore the full course: Up Lt Grade Assistant Teacher 2025