java1.7 AutoCloseable接口和try-with-resources语法示例

半兽人 发表于: 2018-03-16   最后更新时间: 2024-03-18 17:09:39  
{{totalSubscript}} 订阅, 3,095 游览

AutoCloseable接口。

AutoCloseable接口非常简单,只定义了一个close方法:

public interface AutoCloseable {
    void close() Exception;
}

该接口是 JDK 1.7 才新增的一个接口,方法文档注释该方法在try-with-resources语句中会被自动调用,用于自动释放资源。

try-with-resources语句是JDK 1.7中一个新的异常处理机制,更方便简洁的关闭在try-catch语句块中使用的资源,这里不得不提及传统的资源释放方式try-catch-finally:

byte[] b = new byte[1024];

FileInputStream fis = null;

try {
    fis = new FileInputStream("my.txt");
    int data = fis.read();
    while(data != -1) {
        data = fis.read(b);
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (fis != null) {
        try {
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

System.out.println(new String(b));

传统的资源释放方式是利用finally语句块的特性,不管try语句块里是否抛出异常,finally语句块均会被执行,该方式存在一个较隐蔽的问题,先看下面的改造示例:

byte[] b = new byte[1024];

FileInputStream fis = null;

try {
    fis = new FileInputStream("my.txt");
    int data = fis.read();
    while (data != -1) {
        data = fis.read(b);
    }
    throw new RuntimeException();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (Exception e) {
    throw new RuntimeException();
} finally {
    if (fis != null) {
        try {
            fis.close();
            throw new IOException();
        } catch (IOException e) {
            throw new IOException();
        }
    }
}

System.out.println(new String(b));

本意是想捕获RuntimeException的,但是因为finally语句块的特性,抛出的IOException“抑制”了RuntimeException,外界捕获到的是IOException,不便于准确的判断问题所在。因为是根据调用栈向外抛出异常。

如果采用try-with-resources结构:

byte[] b = new byte[1024];

try(FileInputStream fis = new FileInputStream("my.txt")) {
    int data = fis.read();
    while (data != -1) {
        data = fis.read(b);
    }
}

System.out.println(new String(b));

代码非常简洁,而且FileInputStream资源会被自动关闭,且若抛出异常也是内部的确定异常。

byte[] b = new byte[1024];

try (FileInputStream fis = new FileInputStream("my.txt")) {
    int data = fis.read();
    while (data != -1) {
        data = fis.read(b);
    }
    throw new RuntimeException();
}

当try-with-resources结构中抛出一个异常,同时fis被释放时也抛出一个异常,最终向外抛出是try-with-resources内部的异常,反而FileInputStream资源释放时的异常被抑制,刚好和try-catch-finally相反

若在try-with-resources结构中有多个资源需要被释放,只要实现了AutoCloseable接口,均能被自动关闭:

byte[] b = new byte[1024];

try(FileInputStream fis = new FileInputStream("my.txt"); BufferedInputStream bis = new BufferedInputStream(fis);) {
    int data = bis.read();
    while (data != -1) {
        data = fis.read(b);
    }
}

System.out.println(new String(b));

关闭的顺序是创建的顺序的逆序。

Closeable接口继承了AutoCloseable接口,故原有实现Closeable接口的类,均能在try-with-resources结构中使用。

也可以将自定义的类实现AutoCloseable接口,然后在try-with-resources结构中使用。

更新于 2024-03-18
在线,3小时前登录

查看java更多相关的文章或提一个关于java的问题,也可以与我们一起分享文章