The character output stream class hierarchy is shown in Figure 25.4. At the top of the hierarchy is the Writer class that defines methods to write one or more characters to the output stream. The BufferedWriter class provides a buffer for improved I/O performance. The PrintWriter class can be wrapped around either a character or byte output stream and gives the underlying stream access to the print() and println() methods. The OutputStreamWriter class allows you to wrap a character output stream around a byte output stream. The FileWriter class connects an output stream to a file allowing you to write data to the file. Figure 25.4. Character Output Streams
Writer ClassThe Writer class is the abstract superclass for all character output stream classes. It defines methods that are available to all character output streams, including methods to close an output stream, write the contents of a buffer to the stream, or write character or String data to the stream.
The close() method closes the output stream and releases any resources allocated to it. The flush() method causes any data stored in a memory buffer to be written to the stream. The write() method is used to write a single character, the contents of a character array, or a String to the output stream. You can also write a subset of a character array or String to the output stream. BufferedWriter ClassThe BufferedWriter class provides an intermediary buffer for writing data resulting in more efficient I/O performance. Instead of writing characters or Strings immediately to a file, for instance, the character data is first written to the buffer. When the buffer reaches its capacity, its contents are written to the file. The BufferedWriter class defines two public constructors.
A BufferedWriter object is always wrapped around another character output stream. The second constructor version allows you to specify the size of the buffer. The BufferedWriter class overrides some of the Writer class methods but defines no new methods to write character data. To see an example of using a BufferedWriter , see the "Reading and Writing to a File" section later in this chapter. FileWriter ClassThe FileWriter class allows you to connect a character output stream to a file. FileWriter objects can be used on their own, but they are often used in conjunction with a BufferedWriter or PrintWriter object to take advantage of the extra functionality provided by those classes. The File Writer class does not define any new methods, nor does it override any methods inherited from the OutputStreamWriter or Writer classes. The FileWriter class does define five constructors.
The file can be specified either by a File object, a FileDescriptor object, or by providing the name and, if necessary, path of the file. If append is true, data is written starting at the end of the file. The default is false meaning that data is written from the beginning of the file. OutputStreamWriter ClassThe OutputStreamWriter class provides a way to wrap a character output stream around a byte output stream. Character data is converted to bytes and stored in an intermediate buffer before being written to the underlying stream. The OutputStreamWriter class overrides some of the Writer class methods but defines no new methods to write data. The class defines four public constructors.
The first constructor version uses the default character set mapping scheme. The other three versions allow you to specify a character mapping to be used. PrintWriter ClassThe PrintWriter class provides an underlying output stream access to the print() and println() methods. These methods never throw an IOException and do not need to be enclosed in a try block. The Print Writer class is unique in that it can be wrapped around either a character or byte output stream. The four PrintWriter class constructors are as follows .
If autoFlush is true, any call to the println() method will flush the output buffer. In addition to overriding some of the Writer class methods, the PrintWriter class defines the following methods.
The print() method prints a String or a String representation of a primitive type value or Object to the output stream without appending a newline character to the end of the String .
The println() method prints a String or a String representation of a primitive type value or Object to the output stream. A newline character is appended to the end of the String . The no-argument version simply writes a newline to the output stream. Other Writer SubclassesThe Writer class has some other subclasses that you probably won't use much in your scientific and engineering programming work. These include the CharArrayWriter , FilterWriter , PipedWriter , and StringWriter classes. We won't discuss these classes in this chapter. If you want more details, consult the Sun Java doc pages. |