开发者

java: Do I need to close all the streams?

开发者 https://www.devze.com 2023-03-07 02:00 出处:网络
I have a method that reads text from a file; decompression may be required, depending on an input parameter:

I have a method that reads text from a file; decompression may be required, depending on an input parameter:

public static String readText(File inFile, boolean compressed) {
    InputStream in = null;
    InputStreamReader isr = null;
    StringBuilder sb = new StringBuilder();//constant resizing is costly, so set the STRING_SIZE
    try {
        in = new FileInputStream(inFile);
        if (compressed) {
            in = new GZIPInputStream(in);
        }
        isr = new InputStreamReader(in);
        int length = 0;
        char[] cbuf = new char[8 * 1024];
        while ((length = isr.read(cbuf)) != -1) {
            sb.append(cbuf, 0, length);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } fi开发者_JAVA技巧nally {
        try {
            in.close();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
    return sb.toString();
}

It was suggested that I use InputStream like this so it is easier to write, and so that in the end I only have to close one thing. I am still a bit worried this might cause a memory leak. So my question is: does anyone knows if the code above is OK? Or do I have to get back to a dozen of streams and close them one by one in a finally block?

Thanks a lot.


Yes, closing the outermost stream/reader is sufficient.

However, your code has another potential bug: new InputStreamReader(in) will use the platform default encoding, which depends on the OS region/language settings. You should specify the encoding of the text file and use it explicitly in the constructor.


Here's one point to add: see if 'in' is null before calling 'in.close()' as the exception could happen without the first assignment succeeding.

Also, it's good form to only catch possible exceptions (e.g. IOException). That way if you add more code and the IDE tells you that a new exception type isn't handled you can add the proper specific code rather than never hearing about it because the catch (Exception ) which was originally for IOException is also (mishandling?) every other type.


Here's the clean Java 7 way which works for anything that implements AutoCloseable/Closeable:

try (InputStream in = compressed ?
                        new GZIPInputStream(new FileInputStream(inFile))
                      : new FileInputStream(inFile);
     InputStreamReader isr = new InputStreamReader(in))
{
    int length = 0;
    char[] cbuf = new char[8 * 1024];
    while ((length = isr.read(cbuf)) != -1) {
        sb.append(cbuf, 0, length);
    }
}
catch (Exception e) {
    e.printStackTrace();
}

If you're wondering what happens if there's an exception while closing the resource, read about getSuppressedExceptions() which was also added.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号