Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How many close statements are required for nested streams?  RSS feed

 
William Stafford
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given code like this:
File outFile = "some file name";
FileOutputStream fout = new FileOutputStream(outFile);
BufferedOutputStream bout= new BufferedOutputStream(fout);
OutputStreamWriter out = new OutputStreamWriter(bout);

How many close statements are required? In most code I have looked at only the outermost stream is closed. Here it would be 'out'. But occasionally I have seen all three being explicitly closed.

Thanks,
-=beeky
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You only have to close the outermost one; it will close the others for you.
 
William Stafford
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest,

Thanks for the quick reply!

-=beeky
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!