Java之关闭流

我们深知在操作Java流对象后要将流关闭,但往往事情不尽人意,大致有以下几种不能一定将流关闭的写法:

1.在try中关流,而没在finally中关流


  
  
  1. try {
  2. OutputStream out = new FileOutputStream( "");
  3. // ...操作流代码
  4. out.close();
  5. } catch (Exception e) {
  6. e.printStackTrace();
  7. }

正确写法:


 
 
  1. OutputStream out = null;
  2. try {
  3. out = new FileOutputStream( "");
  4. // ...操作流代码
  5. } catch (Exception e) {
  6. e.printStackTrace();
  7. } finally {
  8. try {
  9. if (out != null) {
  10. out.close();
  11. }
  12. } catch (Exception e) {
  13. e.printStackTrace();
  14. }
  15. }

 

2.在关闭多个流时因为嫌麻烦将所有关流的代码丢到一个try中


 
 
  1. OutputStream out = null;
  2. OutputStream out2 = null;
  3. try {
  4. out = new FileOutputStream( "");
  5. out2 = new FileOutputStream( "");
  6. // ...操作流代码
  7. } catch (Exception e) {
  8. e.printStackTrace();
  9. } finally {
  10. try {
  11. if (out != null) {
  12. out.close(); // 如果此处出现异常,则out2流没有被关闭
  13. }
  14. if (out2 != null) {
  15. out2.close();
  16. }
  17. } catch (Exception e) {
  18. e.printStackTrace();
  19. }
  20. }

正确写法:


 
 
  1. OutputStream out = null;
  2. OutputStream out2 = null;
  3. try {
  4. out = new FileOutputStream( "");
  5. out2 = new FileOutputStream( "");
  6. // ...操作流代码
  7. } catch (Exception e) {
  8. e.printStackTrace();
  9. } finally {
  10. try {
  11. if (out != null) {
  12. out.close(); // 如果此处出现异常,则out2流也会被关闭
  13. }
  14. } catch (Exception e) {
  15. e.printStackTrace();
  16. }
  17. try {
  18. if (out2 != null) {
  19. out2.close();
  20. }
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. }

 

 

3.在循环中创建流,在循环外关闭,导致关闭的是最后一个流


 
 
  1. OutputStream out = null;
  2. try {
  3. for ( int i = 0; i < 10; i++) {
  4. out = new FileOutputStream( "");
  5. // ...操作流代码
  6. }
  7. } catch (Exception e) {
  8. e.printStackTrace();
  9. } finally {
  10. try {
  11. if (out != null) {
  12. out.close();
  13. }
  14. } catch (Exception e) {
  15. e.printStackTrace();
  16. }
  17. }

正确写法:


 
 
  1. for ( int i = 0; i < 10; i++) {
  2. OutputStream out = null;
  3. try {
  4. out = new FileOutputStream( "");
  5. // ...操作流代码
  6. } catch (Exception e) {
  7. e.printStackTrace();
  8. } finally {
  9. try {
  10. if (out != null) {
  11. out.close();
  12. }
  13. } catch (Exception e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. }

 

4.在Java7中,关闭流这种繁琐的事情再也不用我们自己敲代码了


 
 
  1. try (OutputStream out = new FileOutputStream( "")){
  2. // ...操作流代码
  3. } catch (Exception e) {
  4. e.printStackTrace();
  5. }

只要实现的自动关闭接口(Closeable)的类都可以在try结构体上定义,java会自动帮我们关闭,及时在发生异常的情况下也会。可以在try结构体上定义多个,用分号隔开即可,如:


 
 
  1. try (OutputStream out = new FileOutputStream( "");OutputStream out2 = new FileOutputStream( "")){
  2. // ...操作流代码
  3. } catch (Exception e) {
  4. throw e;
  5. }

注:Android SDK 20版本对应java7,低于20版本无法使用try-catch-resources自动关流。

 

5.内存流可以不用关闭(关与不关都可以,没影响)

ByteArrayOutputStream和ByteArrayInputStream其实是伪装成流的字节数组(把它们当成字节数据来看就好了),他们不会锁定任何文件句柄和端口,如果不再被使用,字节数组会被垃圾回收掉,所以不需要关闭。

 

6.使用装饰流时,只需要关闭最后面的装饰流即可

装饰流是指通过装饰模式实现的java流,又称为包装流,装饰流只是为原生流附加额外的功能(或效果),java中的缓冲流、桥接流也是属于装饰流。


 
 
  1. InputStream is= new FileInputStream( "C:\\Users\\tang\\Desktop\\记事本.txt");
  2. InputStreamReader isr= new InputStreamReader(is);
  3. BufferedReader br = new BufferedReader(isr);
  4. String string = br.readLine();
  5. System.out.println(string);
  6. try {
  7. br.close(); //只需要关闭最后的br即可
  8. } catch (Exception e) {
  9. e.printStackTrace();
  10. }

装饰流关闭时会调用原生流关闭,请看源码:


 
 
  1. //BufferedReader.java
  2. public void close() throws IOException {
  3. synchronized (lock) {
  4. if (in == null)
  5. return;
  6. try {
  7. in.close(); //这里的in就是原生流
  8. } finally {
  9. in = null;
  10. cb = null;
  11. }
  12. }
  13. }

 
 
  1. //InputStreamReader.java
  2. public void close() throws IOException {
  3. sd.close(); //这里的sd就是原生流的解码器(StreamDecoder),解码器的close会调用原生流的close
  4. }

有这样层层关闭的机制,我们就只需要关闭最外层的流就行了(甚至博主认为,其实只关闭最里层的流也可以,因为装饰流并不持有任何文件句柄和端口,它和内存流一样是个“假流”,当然这只是我的个人推理,不敢保证只关闭最里层的流一定没有问题)。

 

7.关闭流时的顺序问题

两个不相干的流的关闭顺序没有任何影响,如:


 
 
  1. out1 = new FileOutputStream( "");
  2. out2 = new FileOutputStream( "");
  3. //这里的out1和out2谁先关谁后关都一样,没有任何影响

如果两个流有依赖关系,那么你可以像上面说的,只关闭最外层的即可。

如果不嫌麻烦,非得一个个关闭,那么需要先关闭最里层,从里往外一层层进行关闭。

为什么不能从外层往里层逐步关闭?原因上面讲装饰流已经讲的很清楚了,关闭外层时,内层的流其实已经同时关闭了,你再去关内层的流,就会报错。

至于网上说的先声明先关闭,就是这个道理,先声明的是内层,最先申明的是最内层,最后声明的是最外层。

 

8.深究为什么一定要关闭流的原因

一个流绑定了一个文件句柄(或网络端口),如果流不关闭,该文件(或端口)将始终处于被锁定(不能读取、写入、删除和重命名)状态,占用大量系统资源却没有释放。

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值