Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing Java InputStreams

I have some questions about the usage of the close() method when using Java InputStreams. From what I see and read from most developers, you should always explicitly call close() on an InputStream when it is no longer needed. But, today I was looking into using a Java properties file, and every example I have found has something like this:

Properties props = new Properties(); try {     props.load(new FileInputStream("message.properties"));     //omitted. } catch (Exception ex) {} 

With the above example, there is no way to explicitly call close() because the InputStream is unreachable after it is used. I have seen many similar uses of InputStreams even though it seems to contradict what most people say about explicitly closing. I read through Oracle's JavaDocs and it does not mention if the Properties.load() method closes the InputStream. I am wondering if this is generally acceptable or if it is preferred to do something more like the following:

Properties props = new Properties(); InputStream fis = new FileInputStream("message.properties"); try {     props.load(fis);     //omitted. } catch (Exception ex) {     //omitted. } finally {     try {         fis.close();     } catch (IOException ioex) {         //omitted.     } } 

Which way is better and/or more efficient? Or does it really matter?

like image 440
Jason Watkins Avatar asked Oct 21 '10 20:10

Jason Watkins


People also ask

Do I need to close InputStream java?

getResourceAsStream("myconffile. properties"); In docs: The close method of InputStream does nothing.

How do I close an InputStream?

Closing an InputStream You close an InputStream by calling the InputStream close() method. Here is an example of opening an InputStream , reading all data from it, and then closing it: InputStream inputstream = new FileInputStream("c:\\data\\input-text. txt"); int data = inputstream.

Do I need to close OutputStream?

copy(InputStream, OutputStream) must not close the OutputStream . You can use it for example to concatenate different InputStreams and in this case it would be a bad idea if it would close the provided Input- and/or OutputStream. As a rule of thumb any method should close only those steams it opens.

How do I close InputStream after returning?

What you can do is put a close() method on your class which cleans up any open file handlers, connections, etc., and require the user of the class to be responsible for calling close() .


2 Answers

The Properties class wraps the input stream in a LineReader to read the properties file. Since you provide the input stream, it's your responsibility to close it.

The second example is a better way to handle the stream by far, don't rely on somebody else to close it for you.

One improvement you could make is to use IOUtils.closeQuietly()

to close the stream, e.g.:

Properties props = new Properties(); InputStream fis = new FileInputStream("message.properties"); try {     props.load(fis);     //omitted. } catch (Exception ex) {     //omitted. } finally {     IOUtils.closeQuietly(fis); } 
like image 115
Jon Avatar answered Sep 20 '22 15:09

Jon


I would go with a try-with-resources (at least for Java 7+):

Properties props = new Properties();  try(InputStream fis = new FileInputStream("message.properties")) {     props.load(fis);     //omitted. } catch (Exception ex) {     //omitted. } 

The close() call should be automatically called when the try block is exited.

like image 30
Daniel Voina Avatar answered Sep 19 '22 15:09

Daniel Voina