Java Writer

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

Fields

Modifier and TypeFieldDescription
protected ObjectlockThe object used to synchronize operations on this stream.

Constructor

ModifierConstructorDescription
protectedWriter()It creates a new character-stream writer whose critical sections will synchronize on the writer itself.
protectedWriter(Object lock)It creates a new character-stream writer whose critical sections will synchronize on the given object.

Methods

Modifier and TypeMethodDescription
Writerappend(char c)It appends the specified character to this writer.
Writerappend(CharSequence csq)It appends the specified character sequence to this writer
Writerappend(CharSequence csq, int start, int end)It appends a subsequence of the specified character sequence to this writer.
abstract voidclose()It closes the stream, flushing it first.
abstract voidflush()It flushes the stream.
voidwrite(char[] cbuf)It writes an array of characters.
abstract voidwrite(char[] cbuf, int off, int len)It writes a portion of an array of characters.
voidwrite(int c)It writes a single character.
voidwrite(String str)It writes a string.
voidwrite(String str, int off, int len)It writes a portion of a string.

Java Writer Example

Output:

Done

output.txt:

I love my country





Latest Courses