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.