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

      }

    }

  • 相关阅读:
    常用辅助类(ContDownLatch、CyclicBarrier、Semaphore)
    Redis
    SpringBoot
    微服务概述
    数据库 子查询和分页查询
    数据库基础语句,聚合函数,练习
    数据库基础知识

    for循环的类型以及while循环
    C#编程循环练习
  • 原文地址:https://www.cnblogs.com/qq710362441/p/6134837.html
Copyright © 2011-2022 走看看