高效 告别996,开启java高效编程之门 4-5TWR进阶与特殊情况

2021-01-08 11:28

阅读:444

标签:tac   dea   物理   org   ring   throw   情况   nal   file   

1    重点:

1.1  TWR简介

1.2  class反编译查看TWR方式关闭物理流资源

 

 

2    TWR简介

◆Java7引入新特性
◆优雅关闭资源
◆一种Java语法糖

 

 

 

3    class反编译查看TWR方式关闭物理流资源idea内置工具class反编译

 

 

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.imooc.zhangxiaoxi.resource;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.junit.Test;

public class FileCopyTestNew {
    public FileCopyTestNew() {
    }

    @Test
    public void fileCopyTestNew() {
        String fileInputStreamUrl = "lib2/FileCopyTest.java";
        String fileOutputStreamUrl = "targetTest/target2.txt";

        try {
            FileInputStream fileInputStream = new FileInputStream(fileInputStreamUrl);
            Throwable var4 = null;

            try {
                FileOutputStream fileOutputStream = new FileOutputStream(fileOutputStreamUrl);
                Throwable var6 = null;

                try {
                    int conent;
                    try {
                        while((conent = fileInputStream.read()) != -1) {
                            fileOutputStream.write(conent);
                        }
                    } catch (Throwable var22) {
                        var6 = var22;
                        throw var22;
                    }
                } finally {
                    $closeResource(var6, fileOutputStream);
                }
            } catch (Throwable var24) {
                var4 = var24;
                throw var24;
            } finally {
                $closeResource(var4, fileInputStream);
            }
        } catch (FileNotFoundException var26) {
            var26.printStackTrace();
        } catch (IOException var27) {
            var27.printStackTrace();
        }

    }
}

 

 

 

 

 

 

4    课程缺失

 

 

 

5    资源关闭特殊情况

◆资源对象被return的情况下,由调用方关闭
◆ByteArraylnputStream等不需要检查关闭的资源对象,
◆使用Socket获取的InputStream和OutputStream对象不需要关闭

 

高效 告别996,开启java高效编程之门 4-5TWR进阶与特殊情况

标签:tac   dea   物理   org   ring   throw   情况   nal   file   

原文地址:https://www.cnblogs.com/1446358788-qq/p/12967384.html


评论


亲,登录后才可以留言!