流资源关闭try-with-resource

一、直接上代码

  • try-with-resource的写法,jdk1.7新增的

public static void main(String[] args) {
    //try-with-resource用法
    try (FileOutputStream fileOutputStream = new FileOutputStream(“test.txt")){
        System.out.println(“文件写入");
        fileOutputStream.write(1);
    } catch (IOException e) {
        System.out.println("异常处理");
        e.printStackTrace();
    }
}
  • jdk1.7之前的写法

public static void old(){
    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream("text.txt");
        System.out.println("文件写入开始");
        fileOutputStream.write(1);
        System.out.println("文件写入完成");
    } catch (IOException e) {
        System.out.println("文件写入异常处理");
        e.printStackTrace();
    } finally {
        if(fileOutputStream != null){
            try {
                fileOutputStream.close();
            }catch (IOException e){
                System.out.println("流关闭异常处理");
                e.printStackTrace();
            }
        }
    }
}

二、再看看class

  • try-with-resource的写法编译后的class,由此可见,编译器自动生成了try-finally的结构

public static void main(String[] args) {
    try {
        FileOutputStream fileOutputStream = new FileOutputStream("text.txt");
        Throwable var2 = null;
        try {
            System.out.println("文件写入开始");
            fileOutputStream.write(1);
            System.out.println("文件写入完成");
        } catch (Throwable var12) {
            var2 = var12;
            throw var12;
        } finally {
            if (fileOutputStream != null) {
                if (var2 != null) {
                    try {
                        fileOutputStream.close();
                    } catch (Throwable var11) {
                        var2.addSuppressed(var11);
                    }
                } else {
                    fileOutputStream.close();
                }
            }
        }
    } catch (IOException var14) {
        System.out.println("异常处理开始");
        var14.printStackTrace();
        System.out.println("异常处理完成");
    }
}
  • try-with-resource中的异常处理

由上段代码可见

    1.当 fileOutputStream.write(1)抛出异常var12时,fileOutputStream.close()也抛出异常var11,会调用var12.addSuppressed(var11),在var12中记录下var11,然后抛出var12。(jdk1.7 Throwable中新增的suppressedExceptions,控制、抑制异常列表)

    2.当 fileOutputStream.write(1)抛出异常var12时,fileOutputStream.close()正常,抛出var12。

    3.当 fileOutputStream.write(1)正常时,fileOutputStream.close()抛出异常在finally未处理,则抛出流关闭的异常。

        很多程序猿习惯吞掉流关闭的异常,在try-with-resource中就做不到了,如异常处理3中的情况。如使用try-with-resource方式关闭流,写代码的时候需要对流的使用异常及关闭异常一起做处理才可以。

三、AutoCloseable

    jdk1.7增加了AutoCloseable类,且Closeable直接继承了AutoCloseable,之前继承了Closeable的流,在jdk1.7只有都支持try-with-resource的写法

/**
 * 实现了AutoCloseable的类,MyAutoCloseable
 */
public class MyAutoCloseable implements AutoCloseable{

    public void printStr(String str){
        System.out.println("输出:"+str);
    }

    @Override
    public void close() throws Exception {
        System.out.println("流自动关闭了");
    }
}
/**
  * main 测试
 */
public class AutoCloseableTest {
    public static void main(String[] args) {
        try(MyAutoCloseable closeable = new MyAutoCloseable()) {
            closeable.printStr("abc");
        }catch (Exception e){
            System.out.println("异常处理"+e);
        }
    }
}

执行输出结果:

输出:abc
流自动关闭了

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值