user710818
user710818

Reputation: 24288

Do I need to close an InputStream in Java?

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?

Upvotes: 55

Views: 36735

Answers (2)

Boris Strandjev
Boris Strandjev

Reputation: 46963

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.

Upvotes: 43

Michael Borgwardt
Michael Borgwardt

Reputation: 346506

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.

Upvotes: 24

Related Questions