zoukankan      html  css  js  c++  java
  • 不同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();
            }
        }
    

      

  • 相关阅读:
    SpringMVC文件下载
    Servlet3.0文件上传
    SpringMVC拦截器的使用(入门)
    SpringMVC文件上传
    SpringMVC后台数据校验
    SpringMVC@InitBinder使用方法
    C++ this指针
    C++ 析构函数
    C++ 构造函数
    C++ 成员函数的实现
  • 原文地址:https://www.cnblogs.com/csyzlm/p/14437176.html
Copyright © 2011-2022 走看看