filer.java

来自「Java经典例程 从外国一大学计算机教授出版物下载的代码 经典」· Java 代码 · 共 35 行

JAVA
35
字号
package myutilities;

import java.io.*;
import javagently.*;

public class Filer {

  /* The Filer class           by J M Bishop  June 2000
   * ----------------------    Java 1.2
   *                           based on the FileMan class
   *                           from June 1997
   *
   * Provides for a file to be opened, with five tries at a
   * correct file name.
   * Illustrates the use of exceptions in for-try loops..
   */

 public static Stream open (String filename) throws IOException {

    Stream in = new Stream (System.in);

    for (int count = 0; count < 5; count ++) {
      try {
        return new Stream(filename, Stream.READ);
      } catch (FileNotFoundException e) {
          System.out.println(filename+" does not exist.");
          if (count < 4) {
            System.out.println("Try again");
          }
          filename = in.readString();
      }
    }
    throw new FileNotFoundException ();
  }
}

⌨️ 快捷键说明

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