Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android 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: 24219
40
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: 21328
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.
 
They weren't very bright, but they were very, very big. Ad contrast:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!