Open In App

Java Writer Class

Last Updated : 10 Dec, 2025
Comments
Improve
Suggest changes
4 Likes
Like
Report

The Java writer class is an abstract class in the java.io package. It is designed for writing character streams. It provides methods for writing characters, arrays of characters and strings to files, memory, or other output destinations.

writer_
Writer

Declaration of the Writer Class

public abstract class Writer implements Appendable, Closeable, Flushable

Since it’s an abstract class, we can’t instantiate it directly; instead, we use concrete subclasses like FileWriterBufferedWriter or PrintWriter.

Java
import java.io.FileWriter;
import java.io.IOException;

public class Geeks {
    public static void main(String[] args)
    {
        try {
          
            // Create a FileWriter to write to a file named "example.txt"
            FileWriter w = new FileWriter("example.txt");

            // Write a simple message to the file
            w.write("Hello, World!");

            // Close the writer
            w.close();

            System.out.println("Message written");
        }
        catch (IOException e) {
            System.out.println("An error occurred: " + e.getMessage());
        }
    }
}

Output
Message written

Explanation: Creates a FileWriter object to write "Hello, World!" into example.txt. After writing, it closes the stream to ensure data is saved and resources are released.

Constructors of the Writer Class

The Writer class in Java has two protected constructors that allow for the creation of character streams with synchronization capabilities.

  • Protected Writer(): Creates a new character stream that can itself synchronize on the writer.
  • protected Writer(Object obj): Creates a new character stream that can itself synchronize on the given object – ‘obj’.

Example 1: Writing Characters to a File

Java
import java.io.*;

public class GFG{
    public static void main(String[] args)
    {
        try {
            Writer writer = new FileWriter("example.txt");
            writer.write("Hello, Java Writer Class!");
            writer.close();
            System.out.println(
                "Data written successfully.");
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output
Data written successfully.

Output File: example.txt

output
output

Explanation: Creates a FileWriter instance to write text data to example.txt. Writes the message "Hello, Java Writer Class!" and closes the writer to finish safely.

Example 2: Using BufferedWriter for Efficiency

Java
import java.io.*;

public class GFG{
    public static void main(String[] args)
    {
        try (Writer writer = new BufferedWriter(
                 new FileWriter("buffered.txt"))) {
            writer.write(
                "BufferedWriter makes writing more efficient.");
            writer.write(
                "\nIt reduces disk I/O by using a buffer.");
            System.out.println(
                "Data written using BufferedWriter.");
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output
Data written using BufferedWriter.

Output File: buffered.txt

output
output

Explanation: Uses BufferedWriter to improve performance by temporarily storing data before writing it. Writes two lines of text into buffered.txt and automatically closes the stream using try-with-resources.

Example 3: Appending Data to a File

Java
import java.io.*;

public class GFG{
    public static void main(String[] args) {
        try (Writer writer = new FileWriter("example.txt", true)) {
            writer.write("\nThis line was appended later.");
            System.out.println("Data appended successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output
Data appended successfully.

Output File: example.txt

output
Output

Explanation: Opens the existing file example.txt in append mode by passing true to FileWriter. Adds a new line "This line was appended later." without overwriting the previous data.

Java Writer Class Methods

There are certain methods associated with the Java Writer class as mentioned below:

MethodDescription
write(int char)Writes a single character to the character stream.
write(String str)Writes an entire string to the character stream.
write(String str, int offset, int maxlen)Writes a specific portion of the string to the character stream, starting at offset and writing maxlen characters.
write(char[] carray)Writes the entire character array to the character stream.
write(char[] carray, int offset, int maxlen)Writes a specific portion of the character array to the character stream, starting at offset and writing maxlen characters.
close()Closes the character stream after flushing it.
flush()Flushes the Writer stream; ensures that any buffered data is written to the underlying output.
append(char c)Appends a single character to the Writer.
append(CharSequence char_sq)Appends the specified character sequence to the Writer.
append(CharSequence char_sq, int start, int end)Appends a specific subsequence of the given character sequence to the Writer.
Suggested Quiz
3 Questions

What does the Writer class in Java primarily handle?

  • A

    Reading binary data from a file

  • B

    Writing text data to a file

  • C

    Writing binary data to a file

  • D

    Reading text data from a file

Explanation:


Which of the following is a subclass of Writer in Java?

  • A

    FileReader

  • B

    OutputStreamWriter

  • C

    BufferedReader

  • D

    FileInputStream

Explanation:


Why can’t we create an object of the Writer class directly?

  • A

    It has no methods

  • B

    It is deprecated

  • C

    It is abstract

  • D

    It belongs to java.lang

Explanation:

Writer is an abstract class, so it must be used via subclasses like FileWriter or BufferedWriter.

Quiz Completed Successfully
Your Score :   2/3
Accuracy :  0%
Login to View Explanation
1/3 1/3 < Previous Next >

Article Tags :

Explore