Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IO close() method

 
Andy Selador
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly does the close() method do for input and output streams? Please use as little jargon as possible.

I am using Apache POI HSSF to read in an Excel file (this maybe should be on an Apache POI forum, but please tell me what you think). I am trying to figure out when I can close the input stream.

If I read in a file containing objects, (any type), close the file input stream, then read in more objects from the associated object input stream, what happens?

Thanks,

Andy
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andy Selador:
What exactly does the close() method do for input and output streams? Please use as little jargon as possible.

Basically, resources are being freed. It could be memory, it could be file handles, it could be a socket connection. It all depends on the type of stream.


I am using Apache POI HSSF to read in an Excel file (this maybe should be on an Apache POI forum, but please tell me what you think). I am trying to figure out when I can close the input stream.

Whenever you're done with it, i.e. not reading anything more from it. Ideally you want to close it ASAP since you can use the resources (memory mostly with POI) a lot better than on an open stream you're not using anymore.
No matter when you call it, make sure you do call it. Always. But only when you're really done with it. Preferrably, put it in a finally block so that it will be called even if there is an exception:


If I read in a file containing objects, (any type), close the file input stream, then read in more objects from the associated object input stream, what happens?

An IOException will be thrown. It doesn't have to happen for all kinds of input streams, but in most cases it will. Remember this: once you've closed any resource (input stream, output stream, socket, database connection), you have to reopen it before you can use it again (in 99% of the cases anyway). If that's not possible, discard the object completely and create a new one.
 
Andy Selador
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the extensive answer. I only have one last question: You say the close() method should always be called to free the resources. If I don't call it and the program exits, will the resources automatically be freed? I understand the need to save the resources for long running programs or to save memory, but for small, short program, it would appear less important. This applies to flush()ing images... after a program exits, are all resources released?

Thank you for the help.

Andy
 
Campbell Ritchie
Sheriff
Posts: 48433
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andy Selador:
Thanks for the extensive answer. I only have one last question: You say the close() method should always be called to free the resources. If I don't call it and the program exits, will the resources automatically be freed? I understand the need to save the resources for long running programs or to save memory, but for small, short program, it would appear less important. This applies to flush()ing images... after a program exits, are all resources released?

Thank you for the help.

Andy
I think the resources will be freed if you don't close your stream/reader/writer/whatever, but you might find you are not flushing your streams, so you may get incomplete writing to your files, and these files might be corrupted.
Anyway . . . It is like a driving instructor saying "Mirror Signal Manoeuvre". It is a good habit to get into, always closing streams.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic