Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

When is an explicit close() necessary?  RSS feed

 
Paul Ramsden
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When must a stream be explicity closed?

Consider


Does this need to be closed?

What about


I think the first example is safe - the close will be done on finalize.
The second example does need a close.

Am I wrong?
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The safest is to always close all your streams and readers, regardless what the source is. If a stream or reader doesn't need to do anything it will do nothing, but should you ever change the source of the stream / reader you will be happy you added the close now.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!