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 }