Do I need to close an InputStream in Java?
Asked Answered
T

2

55

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?

Tartuffe answered 3/3, 2012 at 16:49 Comment(0)
S
42

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.

Slabber answered 3/3, 2012 at 16:51 Comment(8)
I don't think it will be a FileInputStream in most cases.Demagnetize
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.Slabber
Won't the finalize() method close the resource stream for you?Numbers
@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).Demagnetize
@MichaelBorgwardt you are correct it seems to be ByteArrayInputStream in my experiment.Slabber
@MichaelBorgwardt While I agree that you need to call close(), I think your example is a little extreme. Using an external library is definitely a situation in which you need to call close, but as long as you're using up plenty of memory and not a lot of file handles, the internal libraries should take care of you. Now, it is definitely bad practice NOT to call close, and it can be disastrous if you're making tons of file connections, but I think that finalize() will fix it for just 1 or 2 file streams. However, boris says we're talking about byte arrays streams now :PNumbers
@Ryan Amos: I'm not sure you got the point of my example: the external library was the one failing to call close(), and it caused my program to fail because a single stream still open prevented a file from being deleted. Having the stream closed by finalize() is what made this error possible in the first place because it hides it sometimes. finalize() is a design error that should never have existed.Demagnetize
I happen to be staring right now at a list of 1093 open file descriptors as a result of not calling close when using getResourceAsStream(). Relying on finalize() is not advisable.Closefitting
D
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.

Demagnetize answered 3/3, 2012 at 16:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.