• java-文件流正确关闭资源


    用文件流来拷贝一个文件,用到文件字节输入流(FileInputStream)和文件字节输出流(FileOutputStream),用输入流把字节文件读到缓冲数组中,然后将缓冲数组中的字节写到文件中,就很好的完成了文件的复制操作。

    来,看一下代码

     1         //1.创建源和目标
     2         File srcFile = new File("C:/Users/15626/Desktop/test/123.txt");
     3         File targetFile = new File("C:/Users/15626/Desktop/test/123_copy.txt");
     4         //2.创建输入流和输出流
     5         FileInputStream in = new FileInputStream(srcFile);
     6         FileOutputStream out = new FileOutputStream(targetFile);
     7         //3.输入 输出操作
     8         byte[] buffer = new byte[10];    
     9         int len = 0;
    10         while((len = in.read(buffer)) != -1){
    11             //String str = new String(buffer,0,len);
    12             out.write(buffer, 0, len);;
    13         }        
    14         //4.关闭资源文件
    15         in.close();
    16         out.close();

    完了你会发现出现了

    全是错误,那是因为输出输出可能会出现异常,可能你会直接抛出去,那么怎么正确处理这些异常情况?

    当然是try-catch-finally

     1         FileInputStream in = null;
     2         FileOutputStream out = null;
     3         try{//可能出现问题的代码
     4             //1.创建源和目标
     5             File srcFile = new File("C:/Users/15626/Desktop/test/123.txt");
     6             File targetFile = new File("C:/Users/15626/Desktop/test/123_copy.txt");
     7             //2.创建输入流和输出流
     8             in = new FileInputStream(srcFile);
     9             out = new FileOutputStream(targetFile);
    10             //3.输入 输出操作
    11             byte[] buffer = new byte[10];    
    12             int len = 0;
    13             while((len = in.read(buffer)) != -1){
    14                 //String str = new String(buffer,0,len);
    15                 out.write(buffer, 0, len);;
    16             }        
    17         }catch(Exception e){//处理异常
    18             e.printStackTrace();
    19         }finally{
    20             //4.关闭资源文件
    21             try{
    22                 in.close();
    23                 out.close();                
    24             }catch(Exception e){
    25                 e.printStackTrace();
    26             }
    27         }

    好了,到这里之后异常终于消除了,然而这个程序还是有问题:

      当程序执行到  in = new FileInputStream(srcFile); 的时候,假设有个异常,然后程序后去处理这个异常,根本没有执行下面的程序,然而这时候out还是null,在finally中还关闭了out,本身没有异常,你制造了异常。  那么怎么解决呢?

      那么将它们分开try就ok了:

     

     1         finally{
     2             //4.关闭资源文件
     3             try{
     4                 if(in != null){
     5                     in.close();
     6                 }
     7             }catch(Exception e){
     8                 e.printStackTrace();
     9             }
    10             try{
    11                 if(out != null){
    12                     out.close();                
    13                 }
    14 
    15             }catch(Exception e){
    16                 e.printStackTrace();
    17             }            
    18         }

    到这你会发现程序的主体都在上面,然而下面却处理了这么多异常还有关闭资源,程序变得很难看!然而java7之后,出现了自动关闭资源的机制,我去,这感情好啊,来欣赏一下:

     1         File srcFile = new File("C:/Users/15626/Desktop/test/123.txt");
     2         File targetFile = new File("C:/Users/15626/Desktop/test/123_copy.txt");
     3         try(//打开资源的代码
     4             FileInputStream in = new FileInputStream(srcFile);
     5             FileOutputStream out = new FileOutputStream(targetFile);
     6             ){
     7             //3.输入 输出操作
     8             byte[] buffer = new byte[10];    
     9             int len = 0;
    10             while((len = in.read(buffer)) != -1){
    11                 //String str = new String(buffer,0,len);
    12                 out.write(buffer, 0, len);;
    13             }        
    14         }catch(Exception e){
    15             e.printStackTrace();
    16         }

    嗯,这个新特性很好!

    1 try(打开资源的代码 ){
    2 
    3   可能存在异常的代码
    4 
    5 }catch(){
    6 
    7   处理异常信息
    8 
    9 }
  • 相关阅读:
    Android自定义Dialog效果
    Attempt to invoke virtual method 'void android.app.ActionBar.setTitle的解决方法
    Android 震动 和 停止 代码
    任意手机虚拟按键增加方法
    [vijos]1066弱弱的战壕<线段树>
    [bzoj]1059矩阵游戏<二分图匹配*匈牙利算法>
    [bzoj]1053反质数<暴搜>
    [codevs]1250斐波那契数列<矩阵乘法&快速幂>
    [codevs2597]团伙<并查集>
    [noip模拟赛]虫洞holes<SPFA>
  • 原文地址:https://www.cnblogs.com/tfper/p/9833722.html
Copyright © 2020-2023  润新知