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

  

相关文章:

  • 2021-07-18
  • 2021-12-07
  • 2022-03-03
  • 2021-11-14
  • 2021-09-27
  • 2022-12-23
  • 2021-09-14
  • 2021-10-04
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-04-09
  • 2022-12-23
  • 2021-10-18
  • 2021-11-26
  • 2022-03-10
相关资源
相似解决方案