public abstract class Writer extends Object implements Appendable, Closeable, Flushable
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.
Reader
Modifier and Type | Field and Description |
---|---|
protected Object |
lock
The object used to synchronize access to the writer.
|
Modifier | Constructor and Description |
---|---|
protected |
Writer()
Constructs a new
Writer with this as the object used to
synchronize critical sections. |
protected |
Writer(Object lock)
Constructs a new
Writer with lock used to synchronize
critical sections. |
Modifier and Type | Method and Description |
---|---|
Writer |
append(char c)
Appends the character
c to the target. |
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. |
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. |
abstract void |
write(char[] buf,
int offset,
int count)
Writes
count characters starting at offset in buf
to the target. |
void |
write(int oneChar)
Writes one character to the target.
|
void |
write(String str)
Writes the characters from the specified string to the target.
|
void |
write(String str,
int offset,
int count)
Writes
count characters from str starting at offset to the target. |
protected Object lock
protected Writer()
Writer
with this
as the object used to
synchronize critical sections.protected Writer(Object lock)
Writer
with lock
used to synchronize
critical sections.lock
- the Object
used to synchronize critical sections.NullPointerException
- if lock
is null
.public abstract void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
- if an error occurs while closing this writer.public abstract void flush() throws IOException
flush
in interface Flushable
IOException
- if an error occurs while flushing this writer.public void write(char[] buf) throws IOException
buf
to the target.buf
- the non-null array containing characters to write.IOException
- if this writer is closed or another I/O error occurs.public abstract void write(char[] buf, int offset, int count) throws IOException
count
characters starting at offset
in buf
to the target.buf
- the non-null character array to write.offset
- the index of the first character in buf
to write.count
- the maximum number of characters to write.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.public void write(int oneChar) throws IOException
oneChar
are written.oneChar
- the character to write to the target.IOException
- if this writer is closed or another I/O error occurs.public void write(String str) throws IOException
str
- the non-null string containing the characters to write.IOException
- if this writer is closed or another I/O error occurs.public void write(String str, int offset, int count) throws IOException
count
characters from str
starting at offset
to the target.str
- the non-null string containing the characters to write.offset
- the index of the first character in str
to write.count
- the number of characters from str
to write.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
.public Writer append(char c) throws IOException
c
to the target. This method works the same
way as write(int)
.append
in interface Appendable
c
- the character to append to the target stream.IOException
- if this writer is closed or another I/O error occurs.public Writer append(CharSequence csq) throws IOException
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.append
in interface Appendable
csq
- the character sequence appended to the target.IOException
- if this writer is closed or another I/O error occurs.public Writer append(CharSequence csq, int start, int end) throws IOException
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.append
in interface Appendable
csq
- the character sequence appended to the target.start
- the index of the first char in the character sequence appended
to the target.end
- the index of the character following the last character of the
subsequence appended to the target.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
.