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 3424748
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:30:40+00:00 2026-05-18T06:30:40+00:00

I have always been slightly confused with the amount of different IO implementations in

  • 0

I have always been slightly confused with the amount of different IO implementations in Java, and now that I am completely stuck in my project development, I was taking my time to read up on useful stuff meanwhile.

I have realized that there is no newbie-friendly comparison (apart from short explanation at API for Writer class) between the different subclasses of the Writer class. So I figured I’d fire away the question, what are those different subclasses good for?

For example, I usually use a FileWriter wrapped with a BufferedWriter for my outputs to files but I have always been irritated by the fact that there is no println() like method, and one has to use newLine() every second line (to make the output human readable). PrintWriterhas the println() method but no constructor that supports appending however…

I’d really appreciate if you could give me your two cents from your experience, or a nice guide/how-to you might have stumbled upon.

EDIT: Thanks for the replies everyone, I really appreciate the info passed on here. It’s a bit unfortunate that the whole append() thing ended up being in focus, it merely meant it as an example. My question was mostly referring to the need and use of all the different implementations, which I guess was mentioned somewhat in a couple of the answers.

It’s hard to pick one answer as accepted, since there are three really solid answers, each has contributed to my understanding of the problem. I am gonna have to go with Anon, this time as he’s got the least amount of rep. points (I presume he’s new on SO). He’s 15 answers some of which are really well formulated, and 0 questions asked. Good contribution I’d say, and that is worth promoting.

That being said, ColinD and Jay also provided really good answers, and have pointed out interesting ideas. Especially Jay’s comment about Java automatically wrapping a BufferedWriter was worth noting. Thanks again guys, really appreciated!

  • 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-05-18T06:30:41+00:00Added an answer on May 18, 2026 at 6:30 am

    The java.io classes generally follow the Decorator pattern. So, while PrintWriter does not have the specific constructor you might want, it does have a constructor that takes another Writer, so you can do something like the following:

    FileOutputStream fos = null;
    try
    {
        fos = new FileOutputStream("foo.txt");
        PrintWriter out = new PrintWriter(
                              new BufferedWriter(
                                  new OutputStreamWriter(fos, "UTF-8")));
        // do what you want to do
        out.flush();
        out.close();
    }
    finally
    {
        // quietly close the FileOutputStream (see Jakarta Commons IOUtils)
    }
    

    As a general usage note, you always want to wrap a low-level Writer (eg FileWriter or OutputStreamWriter) in a BufferedWriter, to minimize actual IO operations. However, this means that you need to explicitly flush and close the outermost Writer, to ensure that all content is written.

    And then you need to close the low-level Writer in a finally block, to ensure that you don’t leak resources.

    Edit:

    Looking at MForster‘s answer made me take another look at the API for FileWriter. And I realized that it doesn’t take an explicit character set, which is a Very Bad Thing. So I’ve edited my code snippet to use a FileOutputStream wrapped by an OutputStreamWriter that takes an explicit character set.

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

Sidebar

Related Questions

I have always been kind of confused by threads, and my class right now
I have always been a bit unclear on the type of tasks that should
ListViews have always been my weak point and right now I am practicing putting
I have modified a working Windows service that had always been starting beforehand. After
I have a js function which has, until now, always been the callback for
I have previously been told that I should always use Randomize() before I use
I have been under the assumption for a while that viewDidUnload is always called
I have been stuck on this for a few days now and it is
I have always been of the belief that if a method can throw an
I have always been bugged by the fact that when exiting with revive.el and

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.