IO异常 的处理 test

 1 package com.throwsss;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 import java.io.InputStream;
 9 
10 class Picture{
11     public static void readWrite(){
12     File file = new File("D://abc.jpg");
13     File file2 = new File("F://abc.jpg");
14     InputStream inputStream = null;
15     FileOutputStream fileOutputStream = null;
16     try {
17         inputStream = new FileInputStream(file);
18         fileOutputStream = new FileOutputStream(file2);
19         byte[] bs = new byte[1024];
20         int length = 0;
21         try {
22             while((length = inputStream.read(bs))!=-1){
23                 fileOutputStream.write(bs, 0, length);
24             }
25         } catch (IOException e) {
26             // TODO Auto-generated catch block
27             throw new RuntimeException(e);
28           }
29         } catch (FileNotFoundException e) {
30         // TODO Auto-generated catch block
31           throw new RuntimeException(e);
32         }finally{
33             if(fileOutputStream != null){
34                 try {
35                     fileOutputStream.close();
36                     System.out.println("关闭输出流资源成功...");
37                 } catch (IOException e) {
38                     System.out.println("关闭输出流资源失败...");
39                     throw new RuntimeException(e);
40                 }finally{
41                     if(inputStream != null){
42                         try {
43                             inputStream.close();
44                             System.out.println("关闭输入流对象成功...");
45                         } catch (IOException e) {
46                             System.out.println("关闭输入流对象失敗...");
47                             throw new RuntimeException(e);
48                         }
49                     }
50                 }
51             }
52         }
53     
54     } 
55 }
56 
57 public class Throwtest {
58 
59     public static void main(String[] args) {
60         // TODO Auto-generated method stub
61 
62         Picture picture = new Picture();
63         picture.readWrite();
64     }
65 
66 }

 

posted @ 2015-08-04 23:31  追梦人丶  阅读(269)  评论(0编辑  收藏  举报