Java之关闭流
Java IO包装流如何关闭?
关闭流的实例在下面,往后拖就OK了
问题:
(1)JAVA的IO流使用了装饰模式,关闭最外面的流的时候会自动调用被包装的流的close()方吗?
(2)如果按顺序关闭流,是从内层流到外层流关闭还是从外层到内存关闭?
问题(1)解释:
如下例子代码: FileInputStream is = new FileInputStream("."); BufferedInputStream bis = new BufferedInputStream(is); bis.close(); 从设计模式上看: java.io.BufferedInputStream是java.io.InputStream的装饰类。 BufferedInputStream装饰一个 InputStream 使之具有缓冲功能,is要关闭只需要调用最终被装饰出的对象的 close()方法即可,因为它最终会调用真正数据源对象的 close()方法。 BufferedInputStream的close方法中对InputStream进行了关闭,下面是jdk中附带的源代码: java.io.BufferedInputStream的api: close public void close()throws IOException 关闭此输入流并释放与该流关联的所有系统资源。
因此,可以只调用外层流的close方法关闭其装饰的内层流,验证例子:
public static void main(String[] args) throws Exception { FileOutputStream fos = new FileOutputStream("d:\\a.txt"); OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); BufferedWriter bw = new BufferedWriter(osw); bw.write("java IO close test"); bw.close(); }
验证ok
问题(2)解释:如果不想使用(1)方式关闭流,可以逐个关闭流(可能大家比较习惯吧)
如下例子:
public static void main(String[] args) throws Exception { FileOutputStream fos = new FileOutputStream("d:\\a.txt"); OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); BufferedWriter bw = new BufferedWriter(osw); bw.write("java IO close test"); //从内带外顺序顺序会报异常 fos.close(); osw.close(); bw.close(); }
报出异常:
Exception in thread "main" java.io.IOException: Stream closed
at sun.nio.cs.StreamEncoder.ensureOpen(StreamEncoder.java:26)
at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:99)
at java.io.OutputStreamWriter.write(OutputStreamWriter.java:190)
at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:111)
at java.io.BufferedWriter.close(BufferedWriter.java:246)
at com.my.test.QQ.main(QQ.java:22)
如下例子:
public static void main(String[] args) throws Exception { FileOutputStream fos = new FileOutputStream("d:\\a.txt"); OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); BufferedWriter bw = new BufferedWriter(osw); bw.write("java IO close test"); // 从外到内顺序关闭ok bw.close(); osw.close(); fos.close(); }
验证ok
一般情况下是:先打开的后关闭,后打开的先关闭
另一种情况:看依赖关系,如果流a依赖流b,应该先关闭流a,再关闭流b
例如处理流a依赖节点流b,应该先关闭处理流a,再关闭节点流b
当然完全可以只关闭处理流,不用关闭节点流。处理流关闭的时候,会调用其处理的节点流的关闭方法
如果将节点流关闭以后再关闭处理流,会抛出IO异常;
我们深知在操作Java流对象后要将流关闭,但往往事情不尽人意,大致有以下几种不能一定将流关闭的写法:
1.在try中关流,而没在finally中关流
- try {
- OutputStream out = new FileOutputStream("");
- // ...操作流代码
- out.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
正确写法:
- OutputStream out = null;
- try {
- out = new FileOutputStream("");
- // ...操作流代码
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (out != null) {
- out.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
2.在关闭多个流时因为嫌麻烦将所有关流的代码丢到一个try中
- OutputStream out = null;
- OutputStream out2 = null;
- try {
- out = new FileOutputStream("");
- out2 = new FileOutputStream("");
- // ...操作流代码
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (out != null) {
- out.close();// 如果此处出现异常,则out2流没有被关闭
- }
- if (out2 != null) {
- out2.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
正确写法:
- OutputStream out = null;
- OutputStream out2 = null;
- try {
- out = new FileOutputStream("");
- out2 = new FileOutputStream("");
- // ...操作流代码
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (out != null) {
- out.close();// 如果此处出现异常,则out2流也会被关闭
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- try {
- if (out2 != null) {
- out2.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
3.在循环中创建流,在循环外关闭,导致关闭的是最后一个流
- OutputStream out = null;
- try {
- for (int i = 0; i < 10; i++) {
- out = new FileOutputStream("");
- // ...操作流代码
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (out != null) {
- out.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
正确写法:
- for (int i = 0; i < 10; i++) {
- OutputStream out = null;
- try {
- out = new FileOutputStream("");
- // ...操作流代码
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (out != null) {
- out.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
PS:在Java7中,关闭流这种繁琐的事情再也不用我们自己敲代码了:
- try (OutputStream out = new FileOutputStream("")){
- // ...操作流代码
- } catch (Exception e) {
- e.printStackTrace();
- }
只要实现的自动关闭接口(Closeable)的类都可以在try结构体上定义,java会自动帮我们关闭,及时在发生异常的情况下也会。可以在try结构体上定义多个,用分号隔开即可,如:
- try (OutputStream out = new FileOutputStream("");OutputStream out2 = new FileOutputStream("")){
- // ...操作流代码
- } catch (Exception e) {
- throw e;
- }