java学习-io-trywithresources

对于 try-with-resources 语法结构 since jdk7,其利用 java.lang.AutoCloseable 接口定义, 对于实现当前接口中close方法的 concrete(实现(非抽象)) 类达到自动释放资源的目的

对于try-with-resources 语法使用要求其中的变量必须实现了 AutoCloseable接口并重写了close方法,否则会抛出不合法异常

try-with-resources close 实际是在前端编译阶段进行的字节码提升,由编译阶段自动补全finally 中的资源释放代码;

对于 try-with-resources 语法的特性重点在于精简了代码的编写

 


/*
* Copyright (c) 2020, guoxing, Co,. Ltd. All Rights Reserved
*/
package com.xingguo.nio;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

/**
* TryWithResourceDemo
* {@link AutoCloseable} with try-with-resources
*
* @author guoxing
* @date 2020/12/8 12:41 PM
* @since 1.0.0
*/
public class TryWithResourceDemo {
// 获取 当前项目运行根目录
private static final String USER_DIR = System.getProperty("user.dir");

public static void main(String[] args) {
// 获取当前项目下的readMe文件路径
Path readMePath = Paths.get(USER_DIR, "README.md");
tryWithResources(readMePath);
tryWithFinally(readMePath);
}

/**
* 对于 当前方法 和 {@link #tryWithResources}实际是相同的效果,但 try-with-resources 代码明显在编写时精简了许多
*
* @param readMePath
*/
private static void tryWithFinally(Path readMePath) {
BufferedReader bufferedReader = null;
try {
try {
bufferedReader = Files.newBufferedReader(readMePath);
for (String line = bufferedReader.readLine(); Objects.nonNull(line); line = bufferedReader.readLine()) {
System.out.println(line);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (Objects.nonNull(bufferedReader)) {
bufferedReader.close();
}
}
} catch (IOException e) {
// 捕获资源释放的异常
throw new RuntimeException(e);
}

}

private static void tryWithResources(Path readMePath) {
// 使用buffered reader 读取文件,借助Files工具类生成BufferedReader
/**
* 通过idea class 反编译工具以及 javap 等命令可以看到 当前端编译后jdk字节码提升的代码
* 根据 try -with - resources 语法,在class文件中实际会自动生成 {@link AutoCloseable#close()}相关代码来释放资源
* 通过 javap class文件 中可以看到 invokevirtual(其代表的是属于字节码提升)
* 根据多态的特性,其会调用具体实现类中的close方法
*/
try (BufferedReader bufferedReader = Files.newBufferedReader(readMePath)) {
for (String line = bufferedReader.readLine(); Objects.nonNull(line); line = bufferedReader.readLine()) {
System.out.println(line);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
 

 

 
 
posted @ 2020-12-08 13:06  郭星  阅读(146)  评论(0)    收藏  举报