Closing a Java FileInputStream

前端 未结 9 935
故里飘歌
故里飘歌 2020-12-08 04:29

Alright, I have been doing the following (variable names have been changed):


FileInputStream fis = null;
try
{
    fis = new FileInputStream(file);

    ...         


        
9条回答
  •  盖世英雄少女心
    2020-12-08 05:00

    In most cases, I find it is just better not to catch the IO exceptions, and simply use try-finally:

    final InputStream is = ... // (assuming some construction that can't return null)
    try {
        // process is
        ...
    } finally {
        is.close();
    }
    

    Except for FileNotFoundException, you generally can't "work around" an IOException. The only thing left to do is report an error, and you will typically handle that further up the call stack, so I find it better to propagate the exception.

    Since IOException is a checked exception, you will have to declare that this code (and any of its clients) throws IOException. That might be too noisy, or you might not want to reveal the implementation detail of using IO. In that case, you can wrap the entire block with an exception handler that wraps the IOException in a RuntimeException or an abstract exception type.

    Detail: I am aware that the above code swallows any exception from the try block when the close operation in the finally block produces an IOException. I don't think that is a big problem: generally, the exception from the try block will be the same IOException that causes the close to fail (i.e. it is quite rare for IO to work fine and then fail at the point of closing). If this is a concern, it might be worth the trouble to "silence" the close.

提交回复
热议问题