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

  • Home
  • SEARCH
  • 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 7085143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:23:19+00:00 2026-05-28T07:23:19+00:00

I was helping a friend to write some Java code, who doesn’t know a

  • 0

I was helping a friend to write some Java code, who doesn’t know a lot about Java. So I wrote him some helper functions to easily get things done that are a little quirky in his eyes. One of them is a function, that writes a String to an OutputStream. Have a look:

public void write(String txt, OutputStream out) {
    PrintWriter printer = new PrintWriter(out);
    printer.print(txt);
    printer.close();
}

Now, you can use that easily in different ways to write wherever you want. For example you could do that:

(new StreamHelper()).write("Hello Test", System.out);

Doing that I found out that afterwards System.out.println() doesn’t write anything to the shell anymore. So I think that maybe printer.close() automatically also closed System.out and I wonder how to reactivate it that I can use it after this function is finished again.

Is my assumption correct? (How could I have found out without asking here?)

How can I continue to use System.out after a call to the write() function?

Are there better ways to write such a helper function?

  • 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-28T07:23:19+00:00Added an answer on May 28, 2026 at 7:23 am

    The general contract for OutputStream‘s close:

    public void close()
    throws IOException Closes this output stream and releases any system resources associated with this stream. The general contract
    of close is that it closes the output stream. A closed stream cannot
    perform output operations and cannot be reopened.

    PrintStream‘s

    public void close() Close the stream. This is done by flushing the
    stream and then closing the underlying output stream.

    The only advice I can give you is that you should not write asymmetrical code, that is, don’t delegate the closing of resources your code has created to somewhere else.

    Even if in your case it might seemed sensible to close the wrapper stream, the fact is that you should not because you are closing a stream opened somewhere else.

    In short:

    public void write(String txt, OutputStream out) {
        PrintWriter printer = new PrintWriter(out);
        printer.print(txt);
        printer.flush();
        //it is very unpolite to close someone else's streams!
        //printer.close();
    }
    

    Oh, and by the way, you may want to change the function name to print, rather than write.

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

Sidebar

Related Questions

Thank you for helping. My problem is that for some reason my code doesn't
Was helping a friend who has some 100+ Crystal Reports 2008 reports as part
I'm helping a friend with a java problem. However, we've hit a snag. We're
I was helping a friend with some C++ homework. I warned said friend that
I'm helping a friend do a little jquery, and along with that is some
Right now I am helping a friend flesh out an idea about displaying various
I am helping a friend from first year to prepare his exam on Java.
I'm interested in helping a friend out this summer with some JavaScript trouble he
I'm helping out a friend, writing him a program. He is re-recording the soundtrack
Yesterday I was helping a friend to compile Intel's MKL Java examples. We were

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.