jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Ocurrio un error:java.io.IOException: Stream closed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Ocurrio un error:java.io.IOException: Stream closed" Watch "Ocurrio un error:java.io.IOException: Stream closed" New topic
Author

Ocurrio un error:java.io.IOException: Stream closed

Marcel Chasiguasin
Greenhorn

Joined: Jul 15, 2011
Posts: 12
Dear all,

why is the following code returning this exception Ocurrio un error:java.io.IOException: Stream closed???
Tomasz Sochanski
Ranch Hand

Joined: Jan 13, 2009
Posts: 47

PrintWriter is closed when you call BufferedWriter.flush(), first run bf.flush(), then pw.close().
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
And because your BufferedWriter is wrapped by the PrintWriter, you don't need to call bf.close. It will be done automatically when you call pw.close
Edit: Which is basically what Tomasz said now that I reread it.


Joanne
Marcel Chasiguasin
Greenhorn

Joined: Jul 15, 2011
Posts: 12
Joanne Neal wrote:And because your BufferedWriter is wrapped by the PrintWriter, you don't need to call bf.close. It will be done automatically when you call pw.close
Edit: Which is basically what Tomasz said now that I reread it.


Dear Joanne,

for free memory and close the connection. do I only need execute this code
bf.flush();
bf.close();

or this
pw.flush();
pw.close();
???
Tomasz Sochanski
Ranch Hand

Joined: Jan 13, 2009
Posts: 47

my bet is:

Thanks Joanne for your notice regarding unnecessary call to bf.close().
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Marcel Chasiguasin wrote:for free memory and close the connection. do I only need execute this code
bf.flush();
bf.close();

or this
pw.flush();
pw.close();
???

You only need to call close on the outermost wrapper object. So, as you have a FileWriter wrapped by a BufferedWriter wrapped by a PrintWriter, calling close on your PrintWriter will also close the FileWriter and BufferedWriter.
I think on most writers calling close will automatically do a flush, but it won't do any harm to call it explicitly.
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 294

On related note Java 7 has added automatic resource Management which will automatically closed any resource opened inside try block, no need to call close() explicitly.


SQL Query Interview Question
 
Have you checked out Aspose?
 
subject: Ocurrio un error:java.io.IOException: Stream closed
 
It's not a secret anymore!