首页 > 其他 > 详细

不同JDK版本的流异常处理

时间:2021-02-23 17:39:43      阅读:26      评论:0      收藏:0      [点我收藏+]

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

  

不同JDK版本的流异常处理

原文:https://www.cnblogs.com/csyzlm/p/14437176.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!