Do I need to close an InputStream in Java?

JavaInputstream

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser710818View Question on Stackoverflow
Solution 1 - JavaBoris StrandjevView Answer on Stackoverflow
Solution 2 - JavaMichael BorgwardtView Answer on Stackoverflow