前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java7的try-with-resources声明(转)

Java7的try-with-resources声明(转)

作者头像
会说话的丶猫
发布2020-08-25 15:42:55
5150
发布2020-08-25 15:42:55
举报

看《Effective Java》第三版的时候,看到了其中建议将try-finally替换为try-with-resources。这个语法糖还算有意思,特此成文。

用法辨析

Java库中有很多资源需要手动关闭,比如InputStream、OutputStream、java.sql.Connection等等。在此之前,通常是使用try-finally的方式关闭资源;Java7之后,推出了try-with-resources声明来替代之前的方式。 try-with-resources 声明要求其中定义的变量实现 AutoCloseable 接口,这样系统可以自动调用它们的close方法,从而替代了finally中关闭资源的功能。

举个栗子,下面是一个复制文件的方法,按照原本try-catch-finally的写法:

代码语言:javascript
复制
// 一个简单的复制文件方法。
public static void copy(String src, String dst) {
    InputStream in = null;
    OutputStream out = null;
    try {
        in = new FileInputStream(src);
        out = new FileOutputStream(dst);
        byte[] buff = new byte[1024];
        int n;
        while ((n = in.read(buff)) >= 0) {
            out.write(buff, 0, n);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

可以看出,这种实现非常的丑陋。

下面来看使用了try-with-resources后的效果:

代码语言:javascript
复制
public static void copy(String src, String dst) {
    try (InputStream in = new FileInputStream(src);
         OutputStream out = new FileOutputStream(dst)) {
        byte[] buff = new byte[1024];
        int n;
        while ((n = in.read(buff)) >= 0) {
            out.write(buff, 0, n);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

try-with-resources将会自动关闭try()中的资源,并且将先关闭后声明的资源。

如果不catch IOException就更加清爽了:

代码语言:javascript
复制
public static void copy(String src, String dst) throws IOException {
    try (InputStream in = new FileInputStream(src);
         OutputStream out = new FileOutputStream(dst)) {
        byte[] buff = new byte[1024];
        int n;
        while ((n = in.read(buff)) >= 0) {
            out.write(buff, 0, n);
        }
    }
}

原理分析

那么try-with-resources有什么神奇之处呢?到底做了什么呢?

我们先来看下AutoCloseable接口:

代码语言:javascript
复制
public interface AutoCloseable {
    void close() throws Exception;
}

其中仅有一个close方法,实现AutoCloseable接口的类将在close方法中实现其关闭资源的功能。

try-with-resources其实是个语法糖,它将在编译时编译成关闭资源的代码。我们将上述例子中的代码编译成class文件,再反编译回java文件,就能看到如下代码

代码语言:javascript
复制
public static void copy(String var0, String var1) throws IOException {
    FileInputStream var2 = new FileInputStream(var0);
    Throwable var3 = null;

    try {
        FileOutputStream var4 = new FileOutputStream(var1);
        Throwable var5 = null;

        try {
            byte[] var6 = new byte[1024];

            int var7;
            while((var7 = var2.read(var6)) >= 0) {
                var4.write(var6, 0, var7);
            }
        } catch (Throwable var29) {
            var5 = var29;
            throw var29;
        } finally {
            if (var4 != null) {
                if (var5 != null) {
                    try {
                        // 关闭FileOutputStream
                        var4.close();
                    } catch (Throwable var28) {
                        var5.addSuppressed(var28);
                    }
                } else {
                    var4.close();
                }
            }

        }
    } catch (Throwable var31) {
        var3 = var31;
        throw var31;
    } finally {
        if (var2 != null) {
            if (var3 != null) {
                try {
                    // 关闭FileInputStream
                    var2.close();
                } catch (Throwable var27) {
                    var3.addSuppressed(var27);
                }
            } else {
                var2.close();
            }
        }

    }

}

除却处理异常相关的代码,其实就是调用了资源的close方法。

不过不得不说这个语法糖挺甜,真香。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-08-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 用法辨析
  • 原理分析
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档