56

My code is:

InputStream confFile=classLoader.getResourceAsStream("myconffile.properties"); 

In docs:

The close method of InputStream does nothing.

Does it mean that I don't need close InputStream?

0

2 Answers 2

43

You do need to close the input Stream, because the stream returned by the method you mention is actually FileInputStream or some other subclass of InputStream that holds a handle for a file. If you do not close this stream you have resource leakage.

Sign up to request clarification or add additional context in comments.

8 Comments

I don't think it will be a FileInputStream in most cases.
I should investigate in that, regretfully I do not have any java instance at hand at the moment. However, I agree with you that it is holding a file handle.
Won't the finalize() method close the resource stream for you?
@Ryan Amos: yes - when and if it runs. Which can be an arbitrarily long time later, time enough for you to run out of file handles. Or for a delete or rename operation to fail. Something that actually happened to me (not with a resource stream): I wanted to modify the EXIF metadata of image files. That involves creating a copy of the file with the update meatada, then deleting the original file and renaming the copy to the original file's name. Unfortunately, the EXIF manipulation library did not close the input stream on the original file, which caused the delete to fail (most of the time).
@MichaelBorgwardt you are correct it seems to be ByteArrayInputStream in my experiment.
|
24

No, it does not mean that - because InputStream is an abstract class, and getResourceAsStream() returns a concrete subclass whose close() method does something - most importantly free a file handle.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.