什么是try-with-resources ?

本文详细介绍了Java 1.7引入的try-with-resources语法,这是一种用于简化资源管理,尤其是关闭流资源的方式。在JDK1.7之前,关闭流资源需要繁琐的try-finally结构,而try-with-resources简化了这一过程。在JDK1.9中,进一步优化了该语法,允许在try括号内直接使用已存在的资源对象。文章还强调了使用try-with-resources的要求,即资源对象需要实现AutoCloseable接口。

1,什么是try-with-resources ?

  • try-with-resources 是JDK 1.7 新增的语法糖

2,为什么使用try-with-resources ?

  • 对于可关闭的资源的处理往往很复杂,使用 try-with-resources 语法可以简化代码,利于程序员的编写

例如:在JDK 1.7 之前,对于流我们需要这样操作:

package test2;
import java.io.*;
public class Test4 {
    public static void main(String[] args)  {
        FileOutputStream fos = null;
        FileInputStream fis = null;
        try {
            fos = new FileOutputStream("hwh.txt");
            fis = new FileInputStream("hwh.txt");
            byte[] buff = new byte[10];
            int len;
            while ((len=fis.read(buff)) != -1){
                fos.write(buff,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if(fos != null){
                        try {
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}

可以看到,为了确保流能正常地关闭,我们对流的处理是非常的复杂的。 为了简化代码,在JDK 1.7 时提供了try-with-resources 语法,用法如下:

package test2;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test5 {
    public static void main(String[] args) {
        try(FileOutputStream fos = new FileOutputStream("hwh.txt");
            FileInputStream fis = new FileInputStream("hwh.txt")) {
            byte[] buff = new byte[10];
            int len;
            while ((len=fis.read(buff)) != -1){
                fos.write(buff,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

可以看到,代码简洁了许多。
在使用try-with-resouces 时,需要将创建流资源的语句放到try后面的()中,编译器会自动对流资源进行关闭。

3,JDK 1.9 时对 try-with-resources 的优化

JDK 1.7 时try-with-resources 的缺点:

  • 资源的创建语句必须写在try后面的()中,作用域为try代码块,无法在try-catch之前或之后使用。

在JDK 1.9 时,我们不需要将整个资源的创建语句放入到try后面的()中,只需要将资源对象放到try后面的()中即可。例如一个方法:

class Util{
    static void handle(FileOutputStream fos,FileInputStream fis){
        try(fos;fis) {
            byte[] buff = new byte[10];
            int len;
            while ((len=fis.read(buff)) != -1){
                fos.write(buff,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

4,try-with-resources 的使用要求

  • try后面的()里面放的是需要关闭的资源,并不一定是流资源。资源对象所在的类需要直接或间接地实现Closeable或AutoCloseable接口。
  • try后面的()里可以放一条资源创建语句或一个资源对象;如果需要放多个资源,用;隔开
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我待Java如初恋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值