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

IO流的异常处理

时间:2016-12-05 19:15:04      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:except   关闭   int   rgs   stat   main   虚拟机   ring   file   

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流的异常处理

标签:except   关闭   int   rgs   stat   main   虚拟机   ring   file   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!