Package java.io
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
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:
Reader
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and Type Method Description Writerappend(char c)Appends the charactercto the target.Writerappend(CharSequence csq)Appends the character sequencecsqto the target.Writerappend(CharSequence csq, int start, int end)Appends a subsequence of the character sequencecsqto the target.abstract voidclose()Closes this writer.abstract voidflush()Flushes this writer.voidwrite(char[] buf)Writes the entire character bufferbufto the target.abstract voidwrite(char[] buf, int offset, int count)Writescountcharacters starting atoffsetinbufto the target.voidwrite(int oneChar)Writes one character to the target.voidwrite(String str)Writes the characters from the specified string to the target.voidwrite(String str, int offset, int count)Writescountcharacters fromstrstarting atoffsetto the target.
-
Field Details
-
lock
The object used to synchronize access to the writer.
-
-
Constructor Details
-
Writer
protected Writer()Constructs a newWriterwiththisas the object used to synchronize critical sections. -
Writer
Constructs a newWriterwithlockused to synchronize critical sections.- Parameters:
lock- theObjectused to synchronize critical sections.- Throws:
NullPointerException- iflockisnull.
-
-
Method Details
-
close
Closes this writer. Implementations of this method should free any resources associated with the writer.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if an error occurs while closing this writer.
-
flush
Flushes this writer. Implementations of this method should ensure that all buffered characters are written to the target.- Specified by:
flushin interfaceFlushable- Throws:
IOException- if an error occurs while flushing this writer.
-
write
Writes the entire character bufferbufto the target.- Parameters:
buf- the non-null array containing characters to write.- Throws:
IOException- if this writer is closed or another I/O error occurs.
-
write
Writescountcharacters starting atoffsetinbufto the target.- Parameters:
buf- the non-null character array to write.offset- the index of the first character inbufto write.count- the maximum number of characters to write.- Throws:
IndexOutOfBoundsException- ifoffset < 0orcount < 0, or ifoffset + countis greater than the size ofbuf.IOException- if this writer is closed or another I/O error occurs.
-
write
Writes one character to the target. Only the two least significant bytes of the integeroneCharare written.- Parameters:
oneChar- the character to write to the target.- Throws:
IOException- if this writer is closed or another I/O error occurs.
-
write
Writes the characters from the specified string to the target.- Parameters:
str- the non-null string containing the characters to write.- Throws:
IOException- if this writer is closed or another I/O error occurs.
-
write
Writescountcharacters fromstrstarting atoffsetto the target.- Parameters:
str- the non-null string containing the characters to write.offset- the index of the first character instrto write.count- the number of characters fromstrto write.- Throws:
IOException- if this writer is closed or another I/O error occurs.IndexOutOfBoundsException- ifoffset < 0orcount < 0, or ifoffset + countis greater than the length ofstr.
-
append
Appends the charactercto the target. This method works the same way aswrite(int).- Specified by:
appendin interfaceAppendable- Parameters:
c- the character to append to the target stream.- Returns:
- this writer.
- Throws:
IOException- if this writer is closed or another I/O error occurs.
-
append
Appends the character sequencecsqto the target. This method works the same way asWriter.write(csq.toString()). Ifcsqisnull, then the string "null" is written to the target stream.- Specified by:
appendin interfaceAppendable- Parameters:
csq- the character sequence appended to the target.- Returns:
- this writer.
- Throws:
IOException- if this writer is closed or another I/O error occurs.
-
append
Appends a subsequence of the character sequencecsqto the target. This method works the same way asWriter.writer(csq.subsequence(start, end).toString()). Ifcsqisnull, then the specified subsequence of the string "null" will be written to the target.- Specified by:
appendin interfaceAppendable- Parameters:
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.- Returns:
- this writer.
- Throws:
IOException- if this writer is closed or another I/O error occurs.IndexOutOfBoundsException- ifstart > end,start < 0,end < 0or eitherstartorendare greater or equal than the length ofcsq.
-