Writers
Analogous to the OutputStream class hierarchy is the Unicode Writer class hierarchy shown below. Note that many class names are similar to the OutputStream classes discussed a few pages ago.
The OutputStreamWriter class is used to convert a character stream into a byte stream based on a particular locale and/or platform specfic encoding. BufferedWriter is used to provide efficiency for processing character output. In the BufferedWriter class, the write(String s) method ( inherited from Writer ) writes a String,the newLine() method outputs the platform dependent line separator to the stream, and the flush() method flushes the output.
The FileWriter class implements a character-based stream connection to a file much like FileOutputStream makes a binary stream connection to a file. The constructors for FileWriter are:
For the last constructor listed above, if append is true, then the output will be appended to an existing file, otherwise the file will be overwritten. Here's an example using the FileWriter. .
import java.io.*; class FileWriterExample { public static void main (String args[]) throws IOException { String line = "This is the only line we will write"; BufferedWriter bw = new BufferedWriter( new FileWriter("c:/mydirectory/myfile")); bw.write(line); bw.newLine(); bw.flush(); } }
Next... page 7-18