• 《Effective Java》第9条:try-with-resources优先于try-finally


    220812_《Effective Java》第9条:try-with-resources优先于try-finally

    一、问题

    Java类库中包含许多需要通过调用close来关闭的资源,例如:InputStream、Output Stream和java.sql.Connection。在编程过程中如果没有关闭会产生性能问题。

    二、范例,使用try-finally

    使用try-finally来关闭资源,如下所示:

    public class FirstLineOfFile_Version1 {
        static String firstLineOfFile(String path) throws IOException {
            BufferedReader br = new BufferedReader(new FileReader(path));
            try {
                return br.readLine();
            } finally {
                br.close();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    如果有两个资源,我们会这样来写,但是不推荐这样做。

    public class Copy_Version1 {
        final static int BUFFER_SIZE = 1024;
    
        static void copy(String src, String dst) throws IOException {
            InputStream in = new FileInputStream(src);
            try {
                OutputStream out = new FileOutputStream(dst);
                try {
                    byte[] buf = new byte[BUFFER_SIZE];
                    int n;
                    while ((n = in.read(buf)) > 0) {
                        out.write(buf, 0, n);
                    }
                } finally {
                    out.close();
                }
            } finally {
                in.close();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    这样写都能正确关闭资源,但是不推荐这样写,为什么呢?

    因为在try块和finally块中都会抛出异常。在这种情况下第二个异常会完全抹除第一个异常。在异常堆栈轨迹中就看不到第一个异常的记录。在现实系统中调试会变得异常复杂。

    三、范例,使用try-with-resources

    Java 7引入了try-with-resources语句,解决了上述问题。要使用这个构造的资源,就必须实现AutoClosable接口。如果编写了一个类,如果它代表了是必须被关闭的资源,那么这个类也应该实现AutoClosable接口。下面来重写firstLineFoFile以及copy方法:

    public class FirstLineOfFile_Version2 {
        static String firstLineOfFile(String path) throws IOException {
            try (BufferedReader br = new BufferedReader(new FileReader(path))) {
                return br.readLine();
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    如果调用readLine和close方法抛异常,会抛出第一个异常,第二个异常会被禁止。这些禁止的异常不是被抛弃了也会打印在异常堆栈中。

    public class Copy_Version2 {
        final static int BUFFER_SIZE = 1024;
    
        static void copy(String src, String dst) throws IOException {
            try (InputStream in = new FileInputStream(src);
                 OutputStream out = new FileOutputStream(dst)
            ) {
                byte[] buf = new byte[BUFFER_SIZE];
                int n;
                while ((n = in.read(buf)) > 0) {
                    out.write(buf, 0, n);
                }
    
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    try-with-resources还可以使用catch子句,这样即可以处理异常,又不需要再套用一层代码。

    public class FirstLineOfFile_Version3 {
        static String firstLineOfFile(String path, String defaultVal) {
            try (BufferedReader br = new BufferedReader(new FileReader(path))) {
                return br.readLine();
            } catch (IOException e) {
                return defaultVal;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    四、总结

    在处理必须关闭的资源时,优先考虑try-with-resources。这样写的代码简洁、清晰,产生的异常也更有参考价值。

  • 相关阅读:
    JavaScript基本数据类型
    【GlobalMapper精品教程】012:WGS84转2000地理坐标系与平面坐标系
    位运算::Bitwise operation
    WebGL程序员兼职副业平台推荐
    SpringCloud - Spring Cloud 之 Gateway网关(十三)
    Matlab/simulink基于MPPT风光储微电网建模仿真(持续更新)
    如何从0到1设计积分系统?
    【if 的高阶用法练习题】It‘s time / would rather
    Linux 磁盘管理+实例
    测试工程师必须掌握!!APP测试常见⾯试题及ADB常⽤命令
  • 原文地址:https://blog.csdn.net/ln_ydc/article/details/126310825