Java Writer

Last Updated : 24 Jan 2026

The Writer class in Java is an abstract class used for writing character streams.

In this chapter, we will learn what the Writer class is, why it is used, its declaration, constructors, important methods, and how it works using examples.

What is Writer Class in Java?

The Writer class is an abstract class that is used to write data in the form of characters, and it is used as the base class for all character output stream classes.

It provides common methods for writing characters, strings, and character arrays. Subclasses such as FileWriter, BufferedWriter, and PrintWriter implement this class to provide concrete functionality.

Java Writer Class Declaration

The following declaration shows how the Writer class is defined in Java.

Fields of Writer Class

The Writer class provides a field used for synchronization.

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

Constructors of Writer Class

The Writer class provides protected constructors that are used by its subclasses.

1. Writer()

This constructor creates a new character stream writer whose critical sections synchronize on the writer itself.

Syntax:

Here is the syntax of this constructor:

2. Writer(Object lock)

This constructor creates a new character stream writer whose critical sections synchronize on the specified lock object.

Syntax:

Here is the syntax of this constructor:

Example of Writer Class Constructors

The following example shows how the constructors of the Writer class are used indirectly through subclasses.

Methods of Writer Class

The Writer class provides methods to write characters and strings.

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.

Examples of Java Writer Class

The following examples demonstrate how the Writer class is used through its subclasses.

Example 1: Writing Text to a File Using FileWriter

This example demonstrates how to write text to a file using the FileWriter class, which is a subclass of Writer.

Output:

Done

output.txt:

I love my country

Example 2: Writing Characters Using write(char[]) Method

This example demonstrates how to write a character array to a file using the Writer class.

Output:

Characters written