• 《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。这样写的代码简洁、清晰,产生的异常也更有参考价值。

  • 相关阅读:
    什么时NoSQL
    5个.NET开源且强大的快速开发框架(帮助你提高生产效率)
    秋招入职阿里腾讯大厂,阿里淘系内传322页“Java并发编程核心讲义”学习笔记,轻松拿下大厂offer
    之江实验室: 如何基于 JuiceFS 为超异构算力集群构建存储层 ?
    【数据结构】线性表
    MySQL 快速入门之 MID() 函数详解
    OpenGL运行环境的搭建(GLFW+GLAD+OpenGL以及在Vistual Studio中的 配置)
    海思3559万能平台搭建:YUV格式简介
    【openwrt学习笔记】Dying Gasp功能和pstore功能的配置(高通 ipq95xx)
    程序员内心独白:四年没见的前女友竟成了我的面试官...
  • 原文地址:https://blog.csdn.net/ln_ydc/article/details/126310825