Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

IO close() method

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79632
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic