java中IO流异常处理

 1 import java.io.File;
 2 import java.io.FileInputStream;
 3 import java.io.FileNotFoundException;
 4 import java.io.IOException;
 5 
 6 public class Demo4 {
 7 
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10        
11         getFile();
12     }
13     
14     public static void getFile() {
15         
16         //1.找到目标文件
17         File file = new File("D:\\a.txt");
18         //2.建立通道
19         FileInputStream inputStream = null;
20         try {
21             inputStream = new FileInputStream(file);
22             byte[] b = new byte[1024];
23             try {
24                 int count = inputStream.read(b);
25                 System.out.println(new String(b,0,count));
26             } catch (IOException e) {
27                 
28                 System.out.println("硬盘损坏了,请修理");
29                 
30                 throw new RuntimeException(e);
31             }
32             
33             
34         } catch (FileNotFoundException e) {
35             
36             System.out.println("文件不存在");
37             //提示用户有错误要修改
38             //让后面的代码定制运行
39             //System.exit(0); 不太好,一般是不随意推出虚拟机。
40             throw new RuntimeException(e);  //创建一个运行时异常
41         }finally {
42             
43             try {
44                 inputStream.close();
45             } catch (IOException e) {
46                 System.out.println("关闭失败");
47                 throw new RuntimeException(e);
48             }
49         }
55     }
57 }

 

转载于:https://www.cnblogs.com/chenrenshui/p/6139072.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值