Java WriterLast 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 DeclarationThe following declaration shows how the Writer class is defined in Java. Fields of Writer ClassThe Writer class provides a field used for synchronization.
Constructors of Writer ClassThe 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 ConstructorsThe following example shows how the constructors of the Writer class are used indirectly through subclasses. Methods of Writer ClassThe Writer class provides methods to write characters and strings.
Examples of Java Writer ClassThe following examples demonstrate how the Writer class is used through its subclasses. Example 1: Writing Text to a File Using FileWriterThis 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[]) MethodThis example demonstrates how to write a character array to a file using the Writer class. Output: Characters written Next TopicJava Reader class |
We request you to subscribe our newsletter for upcoming updates.