首页 > 其他 > 详细

IO流的异常处理

时间:2016-12-05 19:13:47      阅读:260      评论:0      收藏:0      [点我收藏+]

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

 

public class Demo4 {


public static void main(String[] args) {
  getFile();
}

public static void getFile() {

//1.找到目标文件
  File file = new File("D:\\a.txt");
//2.建立通道
  FileInputStream inputStream = null;
  try {
    inputStream = new FileInputStream(file);
    byte[] b = new byte[1024];
  try {
    int count = inputStream.read(b);
    System.out.println(new String(b,0,count));
  } catch (IOException e) {

    System.out.println("出错误");

    throw new RuntimeException(e);
  }


  } catch (FileNotFoundException e) {

    System.out.println("文件不存在");
//提示用户有错误要修改
//让后面的代码定制运行
//System.exit(0); 不太好,一般是不随意推出虚拟机。
    throw new RuntimeException(e); //创建一个运行时异常
  }finally {

  try {
    inputStream.close();
  } catch (IOException e) {
    System.out.println("关闭失败");
    throw new RuntimeException(e);
  }
    }

  }

}

IO流的异常处理

原文:http://www.cnblogs.com/qq710362441/p/6134837.html

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