This week's book giveaway is in the Security forum.
We're giving away four copies of Securing DevOps and have Julien Vehent on-line!
See this thread for details.
Win a copy of Securing DevOps this week in the Security forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

nested objects  RSS feed

 
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello. If anybody has a minute, if I initialized a buffered reader like this...

BufferedReader rd = new BufferedReader(new FileReader(file));

and called

rd.close();

does the rd.close() still leave the FileReader stream out there...potentially causing memory problems if the code runs in many iterations? thank you.
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tom,

By convention, close() does the right thing -- i.e., it calls close() on any "nested" streams. All the input and output streams (as well as readers and writers) that are part of the Java standard library behave this way.
 
Sheriff
Posts: 21255
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if they don't they're broken, because FilterInputStream etc all mention that subclasses must close the underlying stream etc as well.
 
Tom Griffith
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much.
 
I'm still in control here. LOOK at this tiny ad!
Thread Boost - a very different sort of advertising
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!