gene b.
gene b.

Reputation: 11974

Java: Closing Streams: Streams non-NULL even after close()

In my finally clause I clean up any streams, e.g.,

            finally // Clean up 
            {
                if (os != null) {
                    try {
                        os.close();
                    }
                    catch (IOException ioe) {
                        logger.warn("Failed to close outputStream", ioe);
                    }
                }
                if (is != null) {
                    try {
                        is.close();
                    }
                    catch (IOException ioe) {
                        logger.warn("Failed to close inputStream", ioe);
                    }
                }

But I see that the Streams remain non-NULL even after closing. So is it wrong to check for NULL? Or do I not see the result of close ?

Upvotes: 0

Views: 544

Answers (1)

Flying Dutch Boy
Flying Dutch Boy

Reputation: 354

The stream "object" is a reference to a instance of a stream. Whether the stream is open or not is part of its state. The close function is a function that runs in the objects state and thus will not affect references to it.

The reference will stay non-NULL until you set it null, but the stream's state is closed meaning that you cant use it anymore.

Upvotes: 5

Related Questions