首页 > 其他 > 详细

IO异常 的处理

时间:2015-08-05 00:36:03      阅读:218      评论:0      收藏:0      [点我收藏+]
 1 package com.throwsss;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.IOException;
 7 import java.io.InputStream;
 8 
 9 /**
10  *  IO异常 的处理      
11  * @author Administrator
12  *用 throw new RuntimeException(e);方法
13  */
14 class Read{
15     public static void readTest(){
16         File file = new File("D://aa.txt");
17         InputStream inputStream = null;
18         try {
19             inputStream = new FileInputStream(file);
20             byte[] bs = new byte[1024];
21             int length = 0;
22             while((length = inputStream.read(bs)) != -1){
23                 String str = new String(bs,0,length);
24                 System.out.println(str);
25             }
26         } catch (FileNotFoundException e) {
27             //e.printStackTrace();
28             throw new RuntimeException(e);
29         } catch (IOException e){
30             //e.printStackTrace();
31             throw new RuntimeException(e);
32         } finally{
33             if(inputStream != null){
34             try {
35                 inputStream.close();
36                 System.out.println("文件釋放成功");
37             } catch (IOException e) {
38                 // TODO Auto-generated catch block
39                 //e.printStackTrace();
40                 System.out.println("文件释放失败");
41                 throw new RuntimeException(e);
42             
43             }
44             }
45         }
46         
47     }
48 }
49 public class Throwss {
50 
51     public static void main(String[] args) {
52         // TODO Auto-generated method stub
53 
54         Read read = new Read();
55         read.readTest();
56     }
57 
58 }

 

IO异常 的处理

原文:http://www.cnblogs.com/fujilong/p/4703403.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!