try-with-resources的使用场景与实际应用

一、开篇,实际try-with-resources使用以下:

/**
     * 将二进制文件读取出来
     *
     * @param filePath 文本绝对路径
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static byte[] getReaderToBinary(String filePath) throws FileNotFoundException, IOException {

        int bufferSize = 4096; // 设置缓冲区大小
        byte buffer[] = new byte[bufferSize]; // 缓冲区字节数组
        File sourceFile = new File(filePath);
        byte[] b = null;
        try(InputStream fis = new FileInputStream(sourceFile);
            BufferedInputStream bis = new BufferedInputStream(fis, bufferSize);
            ) {
            int len = bis.available();
            b = new byte[len];
            bis.read(b, 0, len);
        }

        return b;
    }

上述例子参考地址:html

* 做者:这我的太懒了
     * 来源:CSDN
     * 原文:https://blog.csdn.net/qq_35546153/article/details/83421506
     * 版权声明:本文为博主原创文章,转载请附上博文连接!

以前,采用try-catch-finally,则较为繁琐不直观,甚至偶尔会忘记将其关闭,以下:java

InputStream fis = null;
        BufferedInputStream bis = null;

        try{
            fis = new FileInputStream(sourceFile);
            bis = new BufferedInputStream(fis, bufferSize);

            int len = bis.available();
            b = new byte[len];
            bis.read(b, 0, len);
        }finally {
            if(fis != null){
                fis.close();
            }
            if(bis != null){
                bis.close();
            }
        }

二、使用场景

try-with-resources的用法就是,在try关键字的后面跟一个括号,把须要关闭的资源定义在括号内。在try块执行完以后会自动的释放掉资源。数据库

可是必须注意,并非全部的指望关闭的代码均可以放进其中,只有实现了java.lang.AutoCloseable接口的类,才能够被自动关闭。数组

eg:上述代码示例中的 InputStream,其定义以下:网络

public abstract class InputStream implements Closeable

Closeable,其定义以下(from jdk1.5版本):ide

/**
 * A {@code Closeable} is a source or destination of data that can be closed.
 * The close method is invoked to release resources that the object is
 * holding (such as open files).
 *
 * @since 1.5
 */
public interface Closeable extends AutoCloseable

说明:测试

  • 一、try-with-resource语法是自 JDK7 新增的。
  • 二、其只是一个语法糖:当你将上面代码反编译后会发现,其实对JVM虚拟机而言,它看到的依然是以前的try-catch-finally写法

三、实际使用

场景以 打开了外部资源 居多:this

  • 文件
  • 数据库链接
  • 网络链接

如有自定义的需求:.net

public class CustomResource implements java.lang.AutoCloseable {

    @Override
    public void close() {
        System.out.printf("调用了[%s]的close方法\n", this.getClass().getName());
    }


    public static void main(String[] args) {
        try (CustomResource customResource = new CustomResource();){
            // do something
            System.out.println("do something");
            throw new RuntimeException("测试抛出未知异常");
        }
    }
}

控制台输出以下:code

do something
Exception in thread "main" java.lang.RuntimeException: 测试抛出未知异常
	at com.xxx.main(CustomResource.java:22)
调用了[com.xxx.CustomResource]的close方法

说明:即便try块中抛出异常,也不影响resource的关闭

四、其余要点

注意:

public class CustomResource implements java.lang.AutoCloseable {

    public CustomResource(){
        throw new RuntimeException("构造器异常:"+ this.getClass().getName());
    }
    @Override
    public void close() {
        System.out.printf("调用了[%s]的close方法\n", this.getClass().getName());
        throw new RuntimeException("close方法异常:"+ this.getClass().getName());
    }



    public static void main(String[] args) {
        try (CustomResource customResource = new CustomResource();){
            // do something
            System.out.println("do something");
        } catch (Exception e){
            System.out.println("do catch");
            e.printStackTrace();
        }
    }
}

控制台输出以下:

do catch
java.lang.RuntimeException: 构造器异常:com.xxx.CustomResource
	at com.xxx.CustomResource.<init>(CustomResource.java:13)
	at com.xxx.CustomResource.main(CustomResource.java:24)

解释说明(参考地址:http://www.javashuo.com/article/p-gxawejwn-hv.html):

try-with-resource时,若是对外部资源的处理和对外部资源的关闭均遭遇了异常,“关闭异常”将被抑制,“处理异常”将被抛出,但“关闭异常”并无丢失,而是存放在“处理异常”的被抑制的异常列表中。

相关文章
相关标签/搜索