Java SE 7 更好地管理资源:不仅仅是语法糖 try-with-resources

package com.xiaobu.test.daily.autoCloseAble;

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author xiaobu
 * @version JDK1.8.0_171
 * @date on  2019/7/31 17:33
 * @description
 */
public class AutoClose implements AutoCloseable {

    @Override
    public void close() throws Exception {
        System.out.println(">>> close()");
        throw new RuntimeException("Exception in close()");
    }


    public void work() throws RuntimeException {
        System.out.println(">>> work()");
        throw new RuntimeException("Exception in work()");
    }

    public static void main(String[] args) {
        try {
            writingWithARM();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try (AutoClose autoClose = new AutoClose()) {
            autoClose.work();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            System.out.println("123 = " + 123);
        }
    }



    private static void writingWithARM() throws IOException {
        try (DataOutputStream out
                     = new DataOutputStream(new FileOutputStream("data"));
        ) {
            out.writeInt(666);
            out.writeUTF("Hello");
        }
    }
}

java.lang.RuntimeException: Exception in work()
	at com.xiaobu.test.daily.autoCloseAble.AutoClose.work(AutoClose.java:20)
	at com.xiaobu.test.daily.autoCloseAble.AutoClose.main(AutoClose.java:25)
	Suppressed: java.lang.RuntimeException: Exception in close()
		at com.xiaobu.test.daily.autoCloseAble.AutoClose.close(AutoClose.java:14)
		at com.xiaobu.test.daily.autoCloseAble.AutoClose.main(AutoClose.java:26)
>>> work()
>>> close()
123 = 123

可以看出先执行try块里面的代码,然后再关闭在try里面声明的资源,多个资源用;隔开

3

Socket、OutputStream、InputStream和sql Connection在JDK7之后都实现了AutoCloseAble接口。

1

2

1564626079(1)

示例:

try (
       FileOutputStream out = new FileOutputStream("output");
       FileInputStream  in1 = new FileInputStream(“input1”);
       FileInputStream  in2 = new FileInputStream(“input2”)
   ) {
       // Do something useful with those 3 streams!
   }   // out, in1 and in2 will be closed in any case

代码一

   static void printToFile1(String text, File file) {
       try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
           bw.write(text);
       } catch (IOException ex) {
           // handle ex
       }
   }

代码二

static void printToFile2(String text, File file) {
    try (FileWriter fw = new FileWriter(file);
            BufferedWriter bw = new BufferedWriter(fw)) {
        bw.write(text);
    } catch (IOException ex) {
        // handle ex
    }
}

经查看字节码文件发现代码一FileWriter没有close(),BufferedWriter执行了close()方法。方法二两个对象都执行了close()方法。

参考:

在try-with-resources块中管理多个链接资源的正确习惯吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值