File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes IO close() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "IO close() method" Watch "IO close() method" New topic
Author

IO close() method

Andy Selador
Ranch Hand

Joined: May 02, 2008
Posts: 34
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

Joined: Oct 27, 2005
Posts: 19784
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Andy Selador
Ranch Hand

Joined: May 02, 2008
Posts: 34
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IO close() method