Do I need to close an InputStream in Java?

后端 未结 2 1196
长发绾君心
长发绾君心 2021-01-01 08:30

My code is:

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

In docs:

The close meth

相关标签:
2条回答
  • 2021-01-01 08:58

    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.

    0 讨论(0)
  • 2021-01-01 09:15

    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.

    0 讨论(0)
提交回复
热议问题