• 不同JDK版本的流异常处理


    1、JDK7以前的流异常try-catch处理

        public static void main(String[] args) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream("D:\1.txt");
                int len;
                while ((len = fis.read()) != -1){
                    System.out.println((char) len);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    

    2、JDK7新特性 流异常try-catch处理

    public static void main(String[] args) {
            try(FileInputStream fis = new FileInputStream("D:\1.txt");
                FileOutputStream fos = new FileOutputStream("D:\2.txt")){
                int len;
                while ((len = fis.read()) != -1){
                    System.out.println((char) len);
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    

    3、JDK9新特性 流异常try-catch处理

        public static void main(String[] args) throws FileNotFoundException {
            FileInputStream fis = new FileInputStream("D:\1.txt");
            FileOutputStream fos = new FileOutputStream("D:\2.txt");
            try(fis;fos){
                int len;
                while ((len = fis.read()) != -1){
                    System.out.println((char) len);
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    

      

  • 相关阅读:
    i++ ++i i=i+1 和i+=1
    cmd命令行 端口
    WAS 查看服务状态
    Linux 拷贝
    jar 压缩 解压 war包
    数据结构
    jar包生成exe可执行程序
    03-vant的一些事
    01-watch原理/computed原理
    05-问题集合
  • 原文地址:https://www.cnblogs.com/csyzlm/p/14437176.html
Copyright © 2020-2023  润新知