⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileioexceptionsample2.java

📁 JAVA学习源代码,大家可以好好参考,请多提宝贵意见
💻 JAVA
字号:
//FileIOExceptionSample2.java
import java.io.*;

public class FileIOExceptionSample2{
  public static void main(String args[]) {
    FileInputStream reader = null;

    try {
       int data;
       reader = new FileInputStream("sample.txt");
       System.out.println("\nreading from file sample.txt...");
       while ( (data = reader.read()) != -1) {
        System.out.print( (char) data);
      }
    }
    catch (FileNotFoundException e) {
      System.out.println("\n***FileNotFoundException***\n");
      System.out.println("---Method printStackTrace information---");
      e.printStackTrace();
      System.out.println("---Method toString information---");
      System.out.println(e.toString());
    }
    catch (IOException e1) {
      System.out.println("\n***IOException when reading file***\n");
      System.out.println("---Method printStackTrace information---");
      e1.printStackTrace();
      System.out.println("---Method toString information---");
      System.out.println(e1.toString());
    }
    finally {
      if (reader != null) {
        try {
          System.out.println("\nClosing file sample.txt...");
          reader.close();
        }
        catch (IOException e2) {
          System.out.println("\n***IOException when closing file***\n");
          System.out.println("---Method printStackTrace information---");
          e2.printStackTrace();
          System.out.println("---Method toString information---");
          System.out.println(e2.toString());
        }
      }
      else
        System.out.println("\nThe file sample.txt has not been openned!");
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -