码迷,mamicode.com
首页 > 其他好文 > 详细

IO异常 的处理

时间:2015-08-05 00:38:06      阅读:110      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!