/** ReadFile.java defines ReadFile, a class to simplify Java file reading in * a manner similar to the KeyBoard class in ann.easyio *  Author:  Charles Hoot *  Version: For Hands On Java *  All Rights Reserved. */package hoj;import java.io.*;                                  // BufferedReader, ...public class ReadFile extends Object{  private static String          myString = "";  private  BufferedReader  myReader;  		//unlike Keyboard, there may be many  											//different files that we want to read  											//from    private String myName;					//remember my name  /** ReadFile constructor   *  Returns nothing, but I am opened for use.   */    public ReadFile(String name)  { 	try {  		myReader = new BufferedReader( 					new FileReader(                             name));    }    catch (FileNotFoundException ex) {    	System.err.println("File "  + name + " not found.");    	throw new RuntimeException("File not found");    }    myName = name;  }     /** close the file   *  Precondition:  file is open.   *  Postcondition: file is closes   */     public void close(){ 	try {   		myReader.close();    }    catch (IOException ex) {    	System.err.println("File "  + myName + " had an error on closing.");    	throw new RuntimeException("File not closed properly");    }  }      /** end of file test   *  Precondition:  file is open.   *  Postcondition: reader position is unchanged   *  Returns true if the reader is at the end of the file   */     public boolean IsEOF() {  		return (peekChar() < 1);  }            /** getChar retrieves the next char, white space or not.   *  Precondition:  file is open.   *  Postcondition: The read position has been advanced 1 char.   *  Return:        the next character, white space or not.   */    public char getChar()  {    int myValue = 0;    try    {      myValue = myReader.read();    }     catch (IOException e)    {      System.out.println("File " + myName + ": " + e);    }       return (char) myValue;  }          /** peekChar looks at the next char without reading it   *  Precondition:  file is open.   *  Postcondition: The read position has not advanced.   *  Return:        the character in front of the read position.   */  public char peekChar()  {    int myValue = 0;    try    {      myReader.mark(1);            // mark current read-position      myValue = myReader.read();   // read a char      myReader.reset();            // reset read-position to mark    }     catch (IOException e)    {      System.out.println("File " + myName + ": " + e);    }         return (char) myValue;  }   /** EatWhiteSpace consumes white space chars   *  Precondition:  0 or more white space chars lie before the read position.   *  Postcondition: 0 white space chars lie before the read position.   */  public void EatWhiteSpace()  {    int ch;      try    {      for (;;)      {        myReader.mark(1);          // set mark in buffer        ch = myReader.read();      // read a char        if (ch < 1)                // if EOF, quit          break;        if (!java.lang.Character.isWhitespace((char)ch))        {          myReader.reset();        // if non-WS, move back to mark & quit          break;        }       }     }     catch (IOException e)    {      System.out.println(e);    }   }   /** readChar reads the next non-white-space char.   *  Precondition:  file is open and contains a non-white-space char.   *  Postcondition: The read position has advanced beyond the next   *                   non-white-space character.   *  Return:        the next non-white-space character.   */  public char readChar()  {    EatWhiteSpace();               // eat leading white space    return getChar();              // read the next char  }     /** readWord reads the next 'word' (delimited by white space).   *  Precondition: file is open and contains a 'word'.   *  Postcondition: The read position has advanced beyond the next unbroken   *                   sequence of non-white-space chars    *                   (skipping any leading white space).   *  Return:        the next unbroken sequence of non-white-space chars.   */  public String readWord()  {    int ch;                        // input variable    String myValue = "";           // myValue is initially empty                                    EatWhiteSpace();               // eat leading white space        try    {      for (;;)      {        ch = myReader.read();      // read a char        if (ch < 1 || java.lang.Character.isWhitespace((char)ch))           break;                   // break for eof or white space        myValue += (char) ch;      // append it to myValue      }      }     catch (IOException e)    {      System.out.println(e);    }       return myValue;  }     /** readLine reads from the read position until the next newline.   *  Precondition:  file is open.   *  Postcondition: The read position has advanced beyond the next    *                   newline char.   *  Return:        the sequence of chars from the read position to    *                   that newline.   */     public String readLine()  {    String myValue = "";    try    {      myValue = myReader.readLine();    }      catch (IOException e)    {      System.out.println(e);    }         return myValue;  }     /** readBoolean tries to read the next word as a boolean value.   *           *  Precondition:  file is open.   *  Postcondition: The read position has advanced beyond the next word.   *  Return:         the boolean equivalent of the next word   *                    (true, TRUE, tRuE, ... == true; anything else == false).   */  public boolean readBoolean()  {    myString = readWord();    return new Boolean(myString).booleanValue();  }     /** readByte tries to read the next word as a byte-integer value.   *  Precondition:  file is open and contains a byte value.   *  Postcondition: The read position has advanced beyond the next word.   *  Return:        the byte-int-equivalent of the next word.   */  public byte readByte()  {    myString = readWord();    return Byte.parseByte(myString);  }             /** readShort tries to read the next word as a short-integer value.   *  Precondition:  file is open and contains a short value.   *  Postcondition: The read position has advanced beyond the next word.   *  Return:        the short-int-equivalent of the next word.   */  public short readShort()  {    myString = readWord();    return Short.parseShort(myString);  }             /** readInt tries to read the next word as an integer value.   *  Precondition:  file is open and contains a base-10 int value.   *  Postcondition: The read position has advanced beyond the next word.   *  Return:        the int equivalent of the next word.   */  public int readInt()  {    myString = readWord();    return Integer.parseInt(myString);  }     /** readOctalInt tries to read the next word as an octal integer value.   *  Precondition:  file is open and contains a base-8 int value.   *  Postcondition: The read position has advanced beyond the next word.   *  Return:        the base-8 int equivalent of the next word.   */  public int readOctalInt()  {    myString = readWord();    return Integer.parseInt(myString, 8);  }     /** readHexInt tries to read the next word as a hexadecimal integer value.   *  Precondition:  file is open and contains a base-16 int value.   *  Postcondition:  The read position has advanced beyond the next word.   *  Return:        the base-16 int equivalent of the next word.   */  public int readHexInt()  {    myString = readWord();    if (myString.length() >= 2 && myString.charAt(0) == '0'                              && myString.charAt(1) == 'x')    return Integer.parseInt(myString.substring(2), 16);    else      return Integer.parseInt(myString, 16);  }     /** readLong tries to read the next word as a long-int value.   * Precondition:: file is open and contains a long value.   * Postcondition:  The read position has advanced beyond the next word.   * Return:       the long-int equivalent of the next word.   */  public long readLong()  {    myString = readWord();    return Long.parseLong(myString);  }     /** readFloat tries to read the next word as a float value.   *  Precondition:  file is open and contains a float value.   *  Postcondition: The read position has advanced beyond the next word.   *  Return:        the float equivalent of the next word.   */  public double readFloat()  {    myString = readWord();    return Float.valueOf(myString).floatValue();        }     /** readDouble tries to read the next word as a double value.   *  Precondition:  file is open and contains a double value.   *  Postcondition: The read position has advanced beyond the next word.   *  Return:        the double equivalent of the next word.   */  public double readDouble()  {    myString = readWord();    return Double.valueOf(myString).doubleValue();  }   } 
