Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,

why is the following code returning this exception Ocurrio un error:java.io.IOException: Stream closed???
 
Ranch Hand
Posts: 47
Eclipse IDE Oracle Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PrintWriter is closed when you call BufferedWriter.flush(), first run bf.flush(), then pw.close().
 
Rancher
Posts: 3742
16
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marcel Chasiguasin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 47
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my bet is:

Thanks Joanne for your notice regarding unnecessary call to bf.close().
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic