Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
Creating a Writer Class
Methods of Writer Class
The write() method
The append() method
The flush() method
The close() method
Constructors of Writer Class
protected Writer()
protected Writer(Object obj)
Frequently Asked Questions
Last Updated: Mar 27, 2024

Writer Class in Java

Author Vasu Bansal
0 upvote


In this article, we will discuss the Writer class in Java and see some sample programs that use the Writer class to perform operations in Java. In the next section, we will see the basic syntax/declaration of the Writer class in Java.

Must Read, Multithreading in java, Duck Number in Java


The Writer class defined under the package is an abstract class in Java that is used to write to character streams. Being an abstract class, this class is not directly useful for programmers while writing codes. However, its subclasses play a major role in developing many programs.

The following is the declaration of the Writer class.

public abstract class Writer
   extends Object
      implements Appendable, Closeable, Flushable

Also see,  Swap Function in Java

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Creating a Writer Class

As mentioned earlier, the Writer class is defined under the package We must import the package in order to use the Writer class. There are many subclasses of the Writer class, such as BufferedWriter, StringWriter, InputStreamWriter. All of these classes extend the Writer class. Another subclass named FileWriter extends the InputStreamWriter class, which in turn extends the Writer class.

We will use the FileWriter class to create an object by the following piece of code.

Writer output = new FileWriter();

Similarly, we can use the other classes to create objects of the Writer class.

Methods of Writer Class

There are a number of methods defined in the Writer class. Some of the Writer class methods are discussed below in this section.

The write() method

This method has multiple definitions, depending on the type of arguments supplied. In one format, we take a String of data as input, and in another one, we take a char array as input. In both cases, we write the input data to the Writer.

The append() method

This method takes a single character as input and inserts the character into the writer.

The flush() method

This method is used to write all the data present in the Writer to a specified destination. 

The close() method

This method is used to close the Writer. It is important to close the Writer, and as a good coding practice, you should always close the Writer.

Constructors of Writer Class

The following are the constructors of the Writer class.

protected Writer()

It is used to create a new character stream. It has a special property of automatically synchronising with the Writer itself.

protected Writer(Object obj)

It is used to create a new character stream. It automatically synchronizes on the Object obj that is supplied as an argument. 


The following is a simple program that demonstrates the uses of the Writer class and its associated methods.



public class Main {
    public static void main(String[] args){

            Writer writer_obj = new PrintWriter(System.out);
            // Writing to out stream using write method
            writer_obj.write("Welcome to Coding Ninjas\n");

            // Flush the writer object

            // Using the append method of Writer class
            // Flush after operation is completed

            // Close the Writer object  

        catch(Exception e){

            System.out.println("Exception is: ");



Welcome to Coding Ninjas

Practice by yourself on java online compiler.

Must Read: Java System Out Println and Hashcode Method in Java

Frequently Asked Questions

  1. Does the invocation of a single stream flushing affect the other buffers in the chain?
    Yes, the invocation of a single stream flushing will flush all other buffers in the chain.
  2. What are the fields of the Writer class in Java?
    The protected Object lock is the field of the Writer class in Java. It is used for the synchronization of the operations on the stream.
  3. What are some methods that are inherited from java.lang.Object?
    Some methods that are inherited from java.lang.Object include the clone, finalize, hashCode, toString methods. This list is not exhaustive, and there exist many more methods.


In this article, we have extensively discussed the Writer class in Java and saw a sample program implementing the Writer class. Read the blog, Reader class in Java on the Coding Ninjas Website to learn more about this topic. 

We hope that this blog has helped you enhance your knowledge regarding the Writer class in Java. If you want to learn more, check out our Android Development Course on the Coding Ninjas Website to learn everything you need to know about Android development. Do upvote our blog to help other ninjas grow. Happy Coding!

Previous article
Reader Class in Java
Next article
File Input/Output
Live masterclass