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

📄 inputfile.java

📁 源码为科学出版社出版的英文<java设计模式>(影印版)所用的所有例子程序
💻 JAVA
字号:
import java.awt.*;
import java.io.*;

public class InputFile {
  RandomAccessFile f = null;
  boolean errflag;
  String s = null;
  public InputFile(String fname) {
    errflag = false;
    try {
      //open file
      f = new RandomAccessFile(fname, "r");
    }
    catch (IOException e) {
      //print error if not found
      System.out.println("no file found");
      errflag = true; //and set flag
    }
  }

//-----------------------------------------
  public boolean checkErr() {
    return errflag;
  }

//-----------------------------------------
  public String read() {
//read a single field up to a comma or end of line
    String ret = "";
    if (s == null) { //if no data in string
      s = readLine(); //read next line
    }
    if (s != null) { //if there is data
      s.trim(); //trim off blanks
      int i = s.indexOf(","); //find next comma
      if (i <= 0) {
        ret = s.trim(); //if no commas go to end of line
        s = null; //and null out stored string
      }
      else {
        ret = s.substring(0, i).trim(); //return left of comma
        s = s.substring(i + 1); //save right of comma
      }
    }
    else {
      ret = null;
    }
    return ret; //return string
  }

//-----------------------------------------
  public String readLine() {
    //read in a line from the file
    s = null;
    try {
      s = f.readLine(); //could throw error
    }
    catch (IOException e) {
      errflag = true;
      System.out.println("File read error");
    }
    return s;
  }

//-----------------------------------------
  public void close() {
    try {
      f.close(); //close file
    }
    catch (IOException e) {
      System.out.println("File close error");
      errflag = true;
    }
  }
//-----------------------------------------
}

⌨️ 快捷键说明

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