Character Output Streams


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

graphics/25fig04.gif

Writer Class

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

[View full width]
 
[View full width]
public abstract void close() throws IOException public abstract void flush() throws IOException public void write(int c) throws IOException public void write(char[] cbuf) throws IOException public void write(char[] cbuf, int offset, int length) throws graphics/ccc.gif IOException public void write(String str) throws IOException public void write(String str, int offset, int length) throws graphics/ccc.gif IOException

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 Class

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

 public BufferedWriter(Writer stream) public BufferedWriter(Writer stream, int bufferSize) 

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 Class

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

 public FileWriter(File file) throws IOException public FileWriter(File file, boolean append) throws IOException public FileWriter(FileDescriptor desc) public FileWriter(String name) throws IOException public FileWriter(String name,boolean append) throws IOException 

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 Class

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

[View full width]
 
[View full width]
public OutputStreamWriter(OutputStream stream) public OutputStreamWriter(OutputStream stream, Charset cs) public OutputStreamWriter(OutputStream stream, CharsetDecoder graphics/ccc.gif dec) public OutputStreamWriter(OutputStream stream, String graphics/ccc.gif charsetName) throws UnsupportedEncodingException

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 Class

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

 public PrintWriter(OutputStream stream) public PrintWriter(OutputStream stream, boolean autoFlush) public PrintWriter(Writer stream) public PrintWriter(Writer stream, boolean autoFlush) 

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.

 public void print(boolean b) public void print(char c) public void print(char[] chars) public void print(double d) public void print(float f) public void print(int i) public void print(long l) public void print(Object obj) public void print(String str) 

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 .

 public void println() public void println(boolean b) public void println(char c) public void println(char[] chars) public void println(double d) public void println(float f) public void println(int i) public void println(long l) public void println(Object obj) public void println(String str) 

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 Subclasses

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



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net