Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9171089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:06:56+00:00 2026-06-17T16:06:56+00:00

Is this code BufferedWriter bw = new BufferedWriter(new FileWriter(test.txt)); try { bw.write(test); } finally

  • 0

Is this code

    BufferedWriter bw = new BufferedWriter(new FileWriter("test.txt"));
    try {
        bw.write("test");
    } finally {
        IOUtils.closeQuietly(bw);
    }

safe or not? As far as I understand when we close a BufferedWriter it will flush its buffer to the underlying stream and may fail due to an error. But IOUtils.closeQuietly API says that any exceptions will be ignored.

Is it possible a data loss will go unnoticed due to IOUtils.closeQuietly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:06:57+00:00Added an answer on June 17, 2026 at 4:06 pm

    The code should look like this regarding to the javadoc of closeQuietly():

    BufferedWriter bw = null;
    
    try {
        bw = new BufferedWriter(new FileWriter("test.txt"));
        bw.write("test");
        bw.flush(); // you can omit this if you don't care about errors while flushing
        bw.close(); // you can omit this if you don't care about errors while closing
    } catch (IOException e) {
        // error handling (e.g. on flushing)
    } finally {
        IOUtils.closeQuietly(bw);
    }
    

    closeQuietly() is not intended for general use instead of calling close() directly on a Closable. Its intended use-case is for ensuring the close inside a finally-block – all error handling you need have to be done BEFORE that.

    That means, if you want to react on Exceptions during the call of close() or flush() then you’ve to handle it the normal way. Adding closeQuietly() in your finally-block just ensures the close, e.g. when the flush failed and close was not called in try-block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here is my code try { fstream = new FileWriter(/Applications/AirDrop/contacts.txt); BufferedWriter out = new
I am new in java! I use this code to write data to a
This code is not pretty, but I want to print out the last generated
I have this piece of code which gives to SAXParseError i.e., it is not
I have this code: private static void saveMetricsToCSV(String fileName, double[] metrics) { try {
i am using the following code to write an array to the file: FileWriter
I am using this code to write to a file in java. it has
For some reason this code results in a truncated text.txt file. It should (according
This is the code I'm using to write a file .name to some folders
I made this code to write on a sdcard, now how can I transform

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.