(17)IO中的异常处理

public static void copyImage() throws IOException

{

//找到目标文件

File inFile = new File("D:\1.jpg");

File destFile = new File("E:\1.jpg");

//建立数据的输入输出通道

FileInputStream fileInputStream = new  FileInputStream(inFile);

FileOutputStream fileOutputStream = new FileOutputStream(destFile); //追加数据....

//每新创建一个FileOutputStream的时候,默认情况下FileOutputStream 的指针是指向了文件的开始的位置。 每写出一次,指向都会出现相应移动。

//建立缓冲数据,边读边写

byte[] buf = new byte[1024];

int length = 0 ;

while((length = fileInputStream.read(buf))!=-1){ //不限丢人的输出来第一次我写的时候,read(buf)中的参数buf被我写丢了,好久才发现的错误

fileOutputStream.write(buf,0,length); //写出很多次数据,所以就必须要追加。

}

//关闭资源 原则: 先开后关,后开先关。

fileOutputStream.close();

fileInputStream.close();

}

 1 //输入字节流的异常处理。
 2     public static void readTest()
 3     {
 4         FileInputStream fileInputStream = null;
 5         try
 6         {
 7             //找到目标文件
 8             File file = new File("D:\b.txt");
 9             //建立数据输入通道
10             fileInputStream = new FileInputStream(file);
11             //建立缓冲数组,读取数据
12             byte[] buf = new byte[1024];
13             int length = 0;
14             while((length = fileInputStream.read(buf))!=-1)
15             {
16                 System.out.println(new String(buf, 0, length));
17             }
18         }catch(IOException e){
19             //处理读出异常的代码 首先要阻止后面的代码执行,而且还要通知调用者出错了... throw
20 //            throw e;    //我们一般不会这样做,或者上面函数声明处使用throws 这个样子需要强迫调用者 trycatch
21             throw new RuntimeException(e);    //将IOException包装一层,然后再抛出,这样子做的目的是为了让调用者更加的方便,运行时异常不需要trycatch包围。
22             
23         }finally
24         {
25             //关闭资源
26             try
27             {
28                 if(fileInputStream!=null){    //关闭之前应当先检查,万一之前通道就没有建立成功
29                     fileInputStream.close();//这一块想想必须应当放到finally块重,为什么,因为如果碰到了异常,总不能不把资源释放吧
30                     System.out.println("关闭资源成功过了");                
31                 }
32             } catch (IOException e)
33             {
34                 System.out.println("关闭资源失败");
35                 throw new RuntimeException(e);
36             }
37 
38         }
39     }
40 //拷贝图片是有输入流有输出流 综合在一块做异常处理,值得玩味。
41     public static void copyImage()
42     {
43         FileInputStream fileInputStream = null;
44         FileOutputStream fileOutputStream = null;
45         //找到目标文件
46         try
47         {
48             File inFile = new File("D:\1.jpg");
49             File outFile = new File("E:\1.jpg");
50             
51             //建立输入输出通道
52             fileInputStream = new FileInputStream(inFile);
53             fileOutputStream = new FileOutputStream(outFile);
54             
55             //缓冲数组
56             byte[] buf = new byte[1024];
57             int length = 0;
58             while((length = fileInputStream.read(buf))!=-1)
59             {
60                 fileOutputStream.write(buf, 0, length);
61             }
62         }catch (IOException e)
63         {
64             System.out.println("拷贝图片出错。。。");
65             throw new RuntimeException(e);
66         }finally
67         {
68             try
69             {
70                 if(fileOutputStream!=null)
71                 {
72                     fileOutputStream.close();
73                     System.out.println("关闭资源成功。。。");
74                 }
75             } catch (IOException e)
76             {
77                 System.out.println("关闭资源失败。。。");
78                 throw new RuntimeException(e);
79             }finally
80             {
81                 try
82                 {
83                     if( fileInputStream!=null )
84                     {
85                         fileInputStream.close();
86                         System.out.println("关闭资源成功...");
87                     }
88                 } catch (IOException e)
89                 {
90                     System.out.println("关闭资源失败...");
91                     throw new RuntimeException(e);
92                 }
93             }    
94         }    
95     }
原文地址:https://www.cnblogs.com/OliverZhang/p/6022040.html