in , ,

Java Writer Class

Java Writer Class
Java Writer Class

In this tutorial, we will learn about Java Writer, its subclasses, and its strategies with the help of an example.

The Writer class of the package is an abstract superclass that represents a stream of characters.

Since Writer is an abstract class, it isn’t valuable without anyone else. In any case, its subclasses can be used to write data.

Subclasses of Writer

In order to use the functionality of the Writer, we can use its subclasses. Some of them are:

We will learn about all these subclasses in the next tutorial.

Make a Writer

To make a Writer, we should import the package first. When we import the package, here is the means by which we can make the writer.

// Creates a Writer
Writer output = new FileWriter();

Here, we have made a writer named output using the FileWriter class. It is on the grounds that the Writer is an abstract class. Thus we can’t make an object of Writer.

Note: We can likewise make writers from different subclasses of the Writer class.

Methods of Writer

The Writer class gives various methods that are actualized by its subclasses. Here are a portion of the methods:

  • write(char[] array) – writes the characters from the specified array to the output stream
  • write(String data) – writes the specified string to the writer
  • append(char c) – inserts the specified character to the current writer
  • flush() – forces to write all the data present in the writer to the corresponding destination
  • close() – closes the writer

Example: Writer Using FileWriter

Here is how we can implement the Writer using the FileWriter class.


public class Main {

    public static void main(String args[]) {

        String data = "This is the data in the output file";

        try {
            // Creates a Writer using FileWriter
            Writer output = new FileWriter("output.txt");

            // Writes string to the file

            // Closes the writer

        catch (Exception e) {

In the above example, we have made a writer using the FileWriter class. The writer is connected with the file output.txt

Writer output = new FileWriter("output.txt");

To write data to the output.txt file, we have implemented these methods.

output.write();      // To write data to the file
output.close();      // To close the writer

When we run the program, the output.txt file is filled with the following content.

This is a line of text inside the file.

To learn more, visit Java Writer (official Java documentation).

Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Java Reader Class

Java Reader Class

Java InputStreamReader Class

Java InputStreamReader Class