cs015.SketchySupport
Class FileIO

java.lang.Object
  extended bycs015.SketchySupport.FileIO

public class FileIO
extends java.lang.Object

This class provides easy file input and output operations. The read methods provide word by word and number by number access to any file. The write methods, however, should only be used to create new files and not modifying existing files. Calling openWrite(filename) where filename is an existing file will delete the contents of filename and replace them with whatever new information is entered. It will not append the new information to the end of an existing file. For this reason, openWrite() should only be used to create new files or to replace the information in an existing file. The FileIO class uses sequential file access. This means that it views the file as a sequence of entries or tokens. Each string, int, or double that is written using the write methods of this class makes up its own entry. Each time one of the write methods is called, a new entry is added to the end of the sequence of entries in the file which is currently open for writing. Each time one of the read methods is called, it returns the next entry in the sequence of entries in the file which is currently open for reading.

Author:
Brent Shields

Constructor Summary
FileIO()
           
 
Method Summary
 void closeRead()
          Calling this method closes the current file which is open for reading.
 void closeWrite()
          Calling this method closes the current file which is open for writing.
 boolean hasMoreData()
          This method returns whether there is more data to be read from the file that is currently open for reading.
 void openRead(java.lang.String filename)
          Opens the file specified by 'filename' for reading.
 void openWrite(java.lang.String filename)
          Opens the file specified by 'filename' for writing.
 double readDouble()
          This method reads the next entry in the file and returns it as a double.
 int readInt()
          This method reads the next entry in the file and returns it as an int.
 java.lang.String readString()
          This method reads the next entry in the file and returns it as a string.
 void writeDouble(double d)
          This method adds an entry to the end of the current file open for writing.
 void writeInt(int i)
          This method adds an entry to the end of the current file open for writing.
 void writeString(java.lang.String s)
          This method writes the specified String to the file that is currently open for writing.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileIO

public FileIO()
Method Detail

openWrite

public void openWrite(java.lang.String filename)
Opens the file specified by 'filename' for writing. This method must be called before any of the three write methods can be called. Failure to do so will result in a Null Pointer Exception. Once a file has been opened for writing, every subsequent call to one of the three write methods will write to the file specified by 'filename.' This method can be called multiple times, but once a file has been opened for writing, it must be closed for writing before another file can be opened for writing. Failing to call closeWrite() before opening a different file for writing will result in no output being written to the first file and probably lots of other bad stuff.

Parameters:
filename - the pathname of the file to be opened for writing.

writeString

public void writeString(java.lang.String s)
This method writes the specified String to the file that is currently open for writing. The string will be contained in the file after the entries made to the file by previous calls to the three write methods and before the entries that will be made by subsequent calls to the write methods. There must be a file open for writing when this method is called or the program will terminate with a Null Pointer Exception.


writeInt

public void writeInt(int i)
This method adds an entry to the end of the current file open for writing. This entry is encoded as an int and will have the value of the passed in parameter. There must be a file open for writing when this method is called, the program will terminate with a Null Pointer Exception.


writeDouble

public void writeDouble(double d)
This method adds an entry to the end of the current file open for writing. This entry is encoded as a double and will have the value of the passed in parameter. There must be a file open for writing when this method is called or an error message will be produced and the program will terminate.


openRead

public void openRead(java.lang.String filename)
Opens the file specified by 'filename' for reading. This method must be called before any of the read methods can be called. Calling one of the three 'read' methods before calling openRead() will result in a Null Pointer Exception. Once a file has been opened for reading, every subsequent call to one of the three read methods will read from the file specified by 'filename.' This method can be called multiple times, but once a file has been opened for reading, it must be closed for reading before another file can be opened for reading.

Parameters:
filename - the pathname of the file to be opened for reading.

readString

public java.lang.String readString()
This method reads the next entry in the file and returns it as a string. The programmer must only use this method if they are certain that the entry being read will be a string. The program will terminate with an an error message if there are no more entries in the file to be read. There must be a file open for reading when this method is called or the program will terminate with an error message.

Returns:
the string value of the next entry in the file.

readInt

public int readInt()
This method reads the next entry in the file and returns it as an int. The programmer must only use this method if they are certain that the entry being read will be an int. The program will terminate with an error message if there are no more entries in the file to be read. There must be a file open for reading when this method is called or the program will terminate with an error message.

Returns:
the integer value of the next entry in the file.

readDouble

public double readDouble()
This method reads the next entry in the file and returns it as a double. The programmer must only use this method if they are certain that the entry being read will be a double. The program will terminate with an error message if there are no more entries in the file to be read. There must be a file open for reading when this method is called or an error message will be produced and the program will terminate.

Returns:
the double value of the next entry in the file.

closeWrite

public void closeWrite()
Calling this method closes the current file which is open for writing. A file with pathname 'filename' becomes the current file open for writing when openWrite(filename) is called. closeWrite() should be called as soon as a program is done writing to the current file. Calling this method when there is no file currently open for writing will result in an error message and termination of the program.


closeRead

public void closeRead()
Calling this method closes the current file which is open for reading. A file with pathname 'filename' becomes the current file open for reading when openRead(filename) is called. closeRead() should be called as soon as a program is done reading from the current file. Calling this method when there is no file currently open for reading will result in an error message and termination of the program.


hasMoreData

public boolean hasMoreData()
This method returns whether there is more data to be read from the file that is currently open for reading.