您的位置:首页 > 编程语言 > Java开发

JDK 1.7 java.io 源码学习之AutoCloseable接口和try-with-resources语法

2017-02-22 14:09 627 查看
为了更好的梳理java.io内类之间的关系,梳理了一下包内类之间的关系,现从接口开始阅读。从AutoCloaseable接口开始,这个接口并不在java.io包内,但是它是Closeable接口的父接口,故直接归这边说明了。

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结构中使用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java