Reading Writing Text  «Prev  Next»


Lesson 12 The flush() and close() methods of the Writer class
ObjectiveExamine the methods of the Writer class used to flush and close files.

The flush( ) method


Like output streams, writers may be buffered. To force the write to take place, call the flush() method.
public abstract void flush() throws IOException
If necessary, this flushes the underlying stream as well.

The close( ) method


The close() method closes the writer and releases any resources associated with it.
public abstract void close() throws IOException

Writer

Writer is an abstract class that defines streaming character output. It implements the AutoCloseable, Closeable, Flushable, and Appendable interfaces. All of the methods in this class throw an IOException in the case of errors.
Class Writer
java.lang.Object
java.io.Writer
All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable
Direct Known Subclasses: BufferedWriter, CharArrayWriter, FilterWriter, OutputStreamWriter, PipedWriter, PrintWriter, StringWriter
public abstract class Writer
extends Object
implements Appendable, Closeable, Flushable

Abstract class for writing to character streams. The only methods that a subclass must implement are write(char[], int, int), flush(), and close(). Most subclasses, however, will override some of the methods defined here in order to provide higher efficiency, additional functionality, or both.

Writer Method Summary

Modifier and Type Method and Description
Writer append(char c)
Appends the specified character to this writer.
Writer append(CharSequence csq)
Appends the specified character sequence to this writer.
Writer append(CharSequence csq, int start, int end)
Appends a subsequence of the specified character sequence to this writer.
abstract void close()
Closes the stream, flushing it first.
abstract void flush()
Flushes the stream.
void write (char[] cbuf)
Writes an array of characters.
abstract void write(char[] cbuf, int off, int len)
Writes a portion of an array of characters.
void write(int c)
Writes a single character.
void write(String str)
Writes a string.
void write(String str, int off, int len)
Writes a portion of a string.