Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Writer

public abstract class Writer
extends Object implements Appendable, Closeable, Flushable

java.lang.Object
   ↳ java.io.Writer
Known Direct Subclasses
Known Indirect Subclasses


The base class for all writers. A writer is a means of writing data to a target in a character-wise manner. Most output streams expect the flush() method to be called before closing the stream, to ensure all data is actually written out.

This abstract class does not provide a fully working implementation, so it needs to be subclassed, and at least the write(char[], int, int), close() and flush() methods needs to be overridden. Overriding some of the non-abstract methods is also often advised, since it might result in higher efficiency.

Many specialized readers for purposes like reading from a file already exist in this package.

See also:

Summary

Fields

protected Object lock

The object used to synchronize access to the writer.

Protected constructors

Writer()

Constructs a new Writer with this as the object used to synchronize critical sections.

Writer(Object lock)

Constructs a new Writer with lock used to synchronize critical sections.

Public methods

Writer append(CharSequence csq)

Appends the character sequence csq to the target.

Writer append(CharSequence csq, int start, int end)

Appends a subsequence of the character sequence csq to the target.

Writer append(char c)

Appends the character c to the target.

abstract void close()

Closes this writer.

abstract void flush()

Flushes this writer.

void write(char[] buf)

Writes the entire character buffer buf to the target.

void write(String str)

Writes the characters from the specified string to the target.

abstract void write(char[] buf, int offset, int count)

Writes count characters starting at offset in buf to the target.

void write(String str, int offset, int count)

Writes count characters from str starting at offset to the target.

void write(int oneChar)

Writes one character to the target.

Inherited methods

From class java.lang.Object
From interface java.lang.Appendable
From interface java.io.Closeable
From interface java.io.Flushable
From interface java.lang.AutoCloseable

Fields

lock

Added in API level 1
Object lock

The object used to synchronize access to the writer.

Protected constructors

Writer

Added in API level 1
Writer ()

Constructs a new Writer with this as the object used to synchronize critical sections.

Writer

Added in API level 1
Writer (Object lock)

Constructs a new Writer with lock used to synchronize critical sections.

Parameters
lock Object: the Object used to synchronize critical sections.
Throws
NullPointerException if lock is null.

Public methods

append

Added in API level 1
Writer append (CharSequence csq)

Appends the character sequence csq to the target. This method works the same way as Writer.write(csq.toString()). If csq is null, then the string "null" is written to the target stream.

Parameters
csq CharSequence: the character sequence appended to the target.
Returns
Writer this writer.
Throws
IOException if this writer is closed or another I/O error occurs.

append

Added in API level 1
Writer append (CharSequence csq, 
                int start, 
                int end)

Appends a subsequence of the character sequence csq to the target. This method works the same way as Writer.writer(csq.subsequence(start, end).toString()). If csq is null, then the specified subsequence of the string "null" will be written to the target.

Parameters
csq CharSequence: the character sequence appended to the target.
start int: the index of the first char in the character sequence appended to the target.
end int: the index of the character following the last character of the subsequence appended to the target.
Returns
Writer this writer.
Throws
IOException if this writer is closed or another I/O error occurs.
IndexOutOfBoundsException if start > end, start < 0, end < 0 or either start or end are greater or equal than the length of csq.

append

Added in API level 1
Writer append (char c)

Appends the character c to the target. This method works the same way as write(int).

Parameters
c char: the character to append to the target stream.
Returns
Writer this writer.
Throws
IOException if this writer is closed or another I/O error occurs.

close

Added in API level 1
void close ()

Closes this writer. Implementations of this method should free any resources associated with the writer.

Throws
IOException if an error occurs while closing this writer.

flush

Added in API level 1
void flush ()

Flushes this writer. Implementations of this method should ensure that all buffered characters are written to the target.

Throws
IOException if an error occurs while flushing this writer.

write

Added in API level 1
void write (char[] buf)

Writes the entire character buffer buf to the target.

Parameters
buf char: the non-null array containing characters to write.
Throws
IOException if this writer is closed or another I/O error occurs.

write

Added in API level 1
void write (String str)

Writes the characters from the specified string to the target.

Parameters
str String: the non-null string containing the characters to write.
Throws
IOException if this writer is closed or another I/O error occurs.

write

Added in API level 1
void write (char[] buf, 
                int offset, 
                int count)

Writes count characters starting at offset in buf to the target.

Parameters
buf char: the non-null character array to write.
offset int: the index of the first character in buf to write.
count int: the maximum number of characters to write.
Throws
IndexOutOfBoundsException if offset < 0 or count < 0, or if offset + count is greater than the size of buf.
IOException if this writer is closed or another I/O error occurs.

write

Added in API level 1
void write (String str, 
                int offset, 
                int count)

Writes count characters from str starting at offset to the target.

Parameters
str String: the non-null string containing the characters to write.
offset int: the index of the first character in str to write.
count int: the number of characters from str to write.
Throws
IOException if this writer is closed or another I/O error occurs.
IndexOutOfBoundsException if offset < 0 or count < 0, or if offset + count is greater than the length of str.

write

Added in API level 1
void write (int oneChar)

Writes one character to the target. Only the two least significant bytes of the integer oneChar are written.

Parameters
oneChar int: the character to write to the target.
Throws
IOException if this writer is closed or another I/O error occurs.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.