Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Declaration
3.
Creating a Writer Class
4.
Methods of Writer Class
4.1.
The write() method
4.2.
The append() method
4.3.
The flush() method
4.4.
The close() method
5.
Constructors of Writer Class
5.1.
protected Writer()
5.2.
protected Writer(Object obj)
6.
Example
7.
Frequently Asked Questions
8.
Conclusion
Last Updated: Mar 27, 2024

Writer Class in Java

Author Vasu Bansal
0 upvote

Introduction

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

Declaration

The Writer class defined under the package java.io 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
Bootcamp

Creating a Writer Class

As mentioned earlier, the Writer class is defined under the package java.io. We must import the java.io.Writer 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. 

Example

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

Code:

import java.io.*;

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

            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
            writer_obj.flush();

            // Using the append method of Writer class
           
            writer_obj.append('N');
            writer_obj.append('i');
            writer_obj.append('n');
            writer_obj.append('j');
            writer_obj.append('a');
            writer_obj.append("\n");
           
            // Flush after operation is completed
            writer_obj.flush();

            // Close the Writer object  
            writer_obj.close();

        }
        catch(Exception e){

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

            System.out.println(e);
        }
       
    }
}

Output:

Welcome to Coding Ninjas
Ninja


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.

Conclusion

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