fileioexceptionsample2.java

来自「JAVA学习源代码,大家可以好好参考,请多提宝贵意见」· Java 代码 · 共 48 行

JAVA
48
字号
//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 + =
减小字号Ctrl + -
显示快捷键?