每天一分钟来一颗Java语法糖(五)try-with-resource

本文深入解析Java 1.7引入的try-with-resource语法糖,展示如何简化资源管理,增强代码可读性和安全性。通过实例对比,说明其在文件复制等场景的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

每天一分钟来一颗Java语法糖(五)try-with-resource

try-with-resource引入版本

  • Java1.7增加了新特性:try-with-resource,资源必须定义在try中,若已经在外面定义,则需要一个本地变量。
  • Java1.9 不再要求定义临时变量,可以直接使用外部资源变量。

try-with-resource介绍

程序如果打开外部资源,在使用后需要正确的关闭。Java提供了try-catch-finally机制,未异常等问题提供了保证。Java1.7以后提供了try-with-resource,它比try-catch-finall简便。

try-with-resource注意事项

  • 资源对象必须实现AutoCloseable接口,即实现close方法
    如FileInputStream
    在这里插入图片描述

try-with-resource演示

/**
  * 使用try-catch-finally方式的复制文件
  * @param source
  * @param target
  */
public static void copy(String source, String target) {
    InputStream in = null;
    OutputStream out = null;
    try {
        in = new FileInputStream(source);
        out = new FileOutputStream(target);
        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-resource方式的复制文件
 * @param source
 * @param target
 */
public static void copy(String source, String target) {
    try (InputStream in = new FileInputStream(source);
         OutputStream out = new FileOutputStream(target)) {
        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-resource方式的复制文件,当然也可以不处理异常,异常让调用者自行处理
 * @param source
 * @param target
 */
public static void copy(String source, String target) throws IOException {
    try (InputStream in = new FileInputStream(source);
         OutputStream out = new FileOutputStream(target)) {
        byte[] buff = new byte[1024];
        int n;
        while ((n = in.read(buff)) >= 0) {
            out.write(buff, 0, n);
        }
    }
}

使用try-with-resource后,代码简洁,可读性增强,同时也不用在想着怎么手动关闭资源文件,有时候这些资源文件不关闭,搞不好就是一个线上事故。

try-with-resource其实是语法糖

try-with-resource其实是语法糖,它将在编译时编译成关闭资源的代码。
把刚才try-with-resource编译后的class文件反编译后如下

public static void copy(String source, String target) {
        try {
            InputStream in = new FileInputStream(source);
            Throwable var3 = null;

            try {
                OutputStream out = new FileOutputStream(target);
                Throwable var5 = null;

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

                    int n;
                    while((n = in.read(buff)) >= 0) {
                        out.write(buff, 0, n);
                    }
                } catch (Throwable var31) {
                    var5 = var31;
                    throw var31;
                } finally {
                    if (out != null) {
                        if (var5 != null) {
                            try {
                                out.close();
                            } catch (Throwable var30) {
                                var5.addSuppressed(var30);
                            }
                        } else {
                            out.close();
                        }
                    }

                }
            } catch (Throwable var33) {
                var3 = var33;
                throw var33;
            } finally {
                if (in != null) {
                    if (var3 != null) {
                        try {
                            in.close();
                        } catch (Throwable var29) {
                            var3.addSuppressed(var29);
                        }
                    } else {
                        in.close();
                    }
                }

            }
        } catch (IOException var35) {
            var35.printStackTrace();
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值