hoj.ReadFile

 

ReadFile, a class to simplify Java interaction with a file. It implements all the methods of ann.util.Keyboard plus a constructor and a close method.

 

public ReadFile(String name)

ReadFile constructor

Receives: A string containing a file name.

Postcondition: If the file name is valid, an object has been created that is connected to the file with the given name. Otherwise a RuntimeException will be thrown.

 

public void close(){

Close the file.

Precondition: The file is open.

Postcondition: The file is closed.

 

public char getChar()

getChar retrieves the next char, white space or not.

Precondition: The file is open.

Postcondition: The read position has been advanced 1 char.

Return: the next character, white space or not.

 

public char peekChar()

peekChar looks at the next char without reading it

Precondition: The file is open.

Postcondition: The read position has not advanced.

Return: the character in front of the read position.

 

public void EatWhiteSpace()

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 char readChar()

readChar reads the next non-white-space char.

Precondition: The 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 String readWord()

readWord reads the next 'word' (delimited by white space).

Precondition: The 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 readLine()

readLine reads from the read position until the next newline.

Precondition: The 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 boolean readBoolean()

readBoolean tries to read the next word as a boolean value.

Precondition: The 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 byte readByte()

readByte tries to read the next word as a byte-integer value.

Precondition: The 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 short readShort()

readShort tries to read the next word as a short-integer value.

Precondition: The 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 int readInt()

readInt tries to read the next word as an integer value.

Precondition: The 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 readOctalInt()

readOctalInt tries to read the next word as an octal integer value.

Precondition: The 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 readHexInt()

readHexInt tries to read the next word as a hexadecimal integer value.

Precondition: The 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 long readLong()

readLong tries to read the next word as a long-int value.

Precondition:: The 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 double readFloat()

readFloat tries to read the next word as a float value.

Precondition: The 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 readDouble()

readDouble tries to read the next word as a double value.

Precondition: The 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.

 

 

 


Back to the package documentation index


Back to the Introduction


Copyright 2000 by Prentice Hall. All rights reserved.