com.skylit.io
Class EasyReader

java.lang.Object
  extended bycom.skylit.io.EasyReader

public class EasyReader
extends java.lang.Object

EasyReader provides simple methods for reading the console and * for opening and reading text files. All exceptions are handled * inside the class and are hidden from the user. * *

* Example: * ======= * * EasyReader console = new EasyReader(); * System.out.print("Enter input file name: "); * String fileName = console.readLine(); * * EasyReader inFile = new EasyReader(fileName); * if (inFile.bad()) * { * System.err.println("Can't open " + fileName); * System.exit(1); * } * * String firstLine = inFile.readLine(); * if (!inFile.eof()) // or: if (firstLine != null) * System.out.println("The first line is : " + firstLine); * * System.out.print("Enter the maximum number of integers to read: "); * int maxCount = console.readInt(); * int k, count = 0; * * while (count < maxCount && !inFile.eof()) * { * k = inFile.readInt(); * if (!inFile.eof()) * { * // process or store this number * count++; * } * } * * inFile.close(); // optional * System.out.println(count + " numbers read"); * * * @author Gary Litvin * @version 1.1 *


Field Summary
protected static int CLOSEERROR
           
protected static int EOF
           
protected  int myErrorFlags
           
protected  java.lang.String myFileName
           
protected  java.io.BufferedReader myInFile
           
protected static int OPENERROR
           
protected static int READERROR
           
 
Constructor Summary
EasyReader()
          Constructor.
EasyReader(java.lang.String fileName)
          Constructor.
 
Method Summary
 boolean bad()
          Checks the status of the file * @return true if en error occurred opening or reading the file, * false otherwise
 void close()
          Closes the file
 boolean eof()
          Checks the EOF status of the file * @return true if EOF was encountered in the previous read * operation, false otherwise
 char readChar()
          Reads the next character from a file (any character including * a space or a newline character).
 double readDouble()
          Reads the next double (without validating its format) * @return the number read or 0 if trying to read beyond the EOF
 int readInt()
          Reads the next integer (without validating its format) * @return the integer read or 0 if trying to read beyond the EOF
 java.lang.String readLine()
          Reads from the current position in the file up to and including * the next newline character.
 java.lang.String readWord()
          Skips whitespace and reads the next word (a string of consecutive * non-whitespace characters (up to but excluding the next space, * newline, etc.) * @return the read string or null if trying to read beyond the EOF
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

myFileName

protected java.lang.String myFileName

myInFile

protected java.io.BufferedReader myInFile

myErrorFlags

protected int myErrorFlags

OPENERROR

protected static final int OPENERROR
See Also:
Constant Field Values

CLOSEERROR

protected static final int CLOSEERROR
See Also:
Constant Field Values

READERROR

protected static final int READERROR
See Also:
Constant Field Values

EOF

protected static final int EOF
See Also:
Constant Field Values
Constructor Detail

EasyReader

public EasyReader()
Constructor. Prepares console (System.in) for reading


EasyReader

public EasyReader(java.lang.String fileName)
Constructor. opens a file for reading * @param fileName the name or pathname of the file

Method Detail

close

public void close()
Closes the file


bad

public boolean bad()
Checks the status of the file * @return true if en error occurred opening or reading the file, * false otherwise


eof

public boolean eof()
Checks the EOF status of the file * @return true if EOF was encountered in the previous read * operation, false otherwise


readChar

public char readChar()
Reads the next character from a file (any character including * a space or a newline character). * @return character read or null character * (unicode 0) if trying to read beyond the EOF


readLine

public java.lang.String readLine()
Reads from the current position in the file up to and including * the next newline character. The newline character is thrown away * @return the read string (excluding the newline character) or * null if trying to read beyond the EOF


readWord

public java.lang.String readWord()
Skips whitespace and reads the next word (a string of consecutive * non-whitespace characters (up to but excluding the next space, * newline, etc.) * @return the read string or null if trying to read beyond the EOF


readInt

public int readInt()
Reads the next integer (without validating its format) * @return the integer read or 0 if trying to read beyond the EOF


readDouble

public double readDouble()
Reads the next double (without validating its format) * @return the number read or 0 if trying to read beyond the EOF