Class WriteFile

java.lang.Object
  |
  +--WriteFile

public class WriteFile
extends java.lang.Object

This class provides a set of simplified write-only text file methods that hide some of the details of file streams for new programmers.


Field Summary
static boolean APPEND
           
 
Constructor Summary
WriteFile()
          Constructs a new WriteFile object and sets the file to null.
 
Method Summary
 void closeFile()
          Closes the current WriteFile and sets the file pointer to null.
 boolean openFile(java.lang.String name)
          Opens a new write-only text file for storing data.
 boolean openFile(java.lang.String name, boolean append)
          Opens a new write-only text file for appending data.
 void write(java.lang.Object o)
          Writes a single object to a file without putting an end-of-line (EOL) character after the object.
 void writeLine(java.lang.Object o)
          Writes a complete line of text to an open text file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

APPEND

public static final boolean APPEND
Constructor Detail

WriteFile

public WriteFile()
Constructs a new WriteFile object and sets the file to null. Example usage:
WriteFile wf = new WriteFile();
Creates a new WriteFile object for subsequent usage (see openFile() method).
Method Detail

openFile

public boolean openFile(java.lang.String name)
Opens a new write-only text file for storing data. Name is the name of a file in the working directory, or a full path and file name. If the file does not exist, it is created. If the file already exist it will be overwritten. To append text to an existing file, use the alternate open method.

Once a WriteFile object is created (as per its constructor) it is ready for opening. Opening a file, sets the file pointer to the beginning position. Usage:

boolean success = wf.openFile("myfile.txt");

Returns: true if file is successfully opened, false otherwise.


openFile

public boolean openFile(java.lang.String name,
                        boolean append)
Opens a new write-only text file for appending data. Name is the name of a file in the working directory, or a full path and file name. If the file does not exist, it is created. If the file already exist data will be appended to the end.

Once a WriteFile object is created (as per its constructor) it is ready for opening. Opening a file, sets the file pointer to the next position where data can be written. Usage:

boolean success = wf.openFile("myfile.txt", APPEND);
where APPEND is a predefined boolean value set to true.

Returns: true if file is successfully opened, false otherwise.


closeFile

public void closeFile()
Closes the current WriteFile and sets the file pointer to null. Once a file is closed, the WriteFile object may be reused to open a new file.

write

public void write(java.lang.Object o)
Writes a single object to a file without putting an end-of-line (EOL) character after the object. This method can be used to write single characters or digits. For example:
char someChar = 'N';
Character c = new Character(someChar);
wf.write(c);
results in a text file such as:
Some text on this line<EOL>
N
^
<EOF>
Where the caret represents the file marker where the next object will be written.

writeLine

public void writeLine(java.lang.Object o)
Writes a complete line of text to an open text file. The line is ended with an end-of-line (EOL) marker. The object passed to the method is written according to its type. This method will work for any object for which a toString() method produces some reasonable results.