zoukankan      html  css  js  c++  java
  • IO流的异常处理

    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);
      }
        }

      }

    }

  • 相关阅读:
    微信推送
    PS学习笔记
    汇编学习笔记
    JAVA学习笔记
    数组作为参数被传递,以及随机数的使用。
    [转]Win7系统中Telnet服务的安装和启动
    电脑高手学习笔记
    Android13.9.15
    C语言9.12
    《将博客搬至CSDN》
  • 原文地址:https://www.cnblogs.com/qq710362441/p/6134837.html
Copyright © 2011-2022 走看看