Open In App

Java Writer Class

Last Updated : 28 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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. Since it is an abstract class, we cannot create an instance of it directly. Instead, we will use one of its concrete subclasses such as FileWriterBufferedWriter or PrintWriter.

Example: The below Java program demonstrates the working of the FileWriter class to write data to a text file.

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

Declaration of the Writer Class

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

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.

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

Java Writer Class Methods

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

1. write(int char) 

  • It writes a single character to the character stream.
  • Characters being written are contained in 16 lower bits of the 'char' integer value, the rest of the 16 higher bits are ignored by the method. 

Syntax:

public void write(int char)

  • Parameter: The integer value of the character to be written.
  • Return type: This method does not return any value
  • Exception: Throws IOException if an I/O error occurs during writing.

2. write(String str)

It writes a string to the character stream. 

Syntax:

public void write(String str)

  • Parameter: string to be written to the character stream.
  • Return type: This method does not return any value
  • Exception: Throws IOException if an I/O error occurs during writing.

3. write(String str, int offset, int maxlen) 

It writes some part of the string to the character stream. 

Syntax:

public void write(String str, int offset, int maxlen)

Parameter:

  1. str: string to be written to the character stream.
  2. offset: start position of the String
  3. maxlen: maximum length upto which string has to written

Return type: This method does not return any value

Exception: Throws IndexOutOfBoundsException if the offset or maxlen is invalid and IOException if an I/O error occurs.

4. write(char[] carray) 

It writes character array to the character stream. 

Syntax:

 public void write(char[] carray)

  • Parameter: Thecharacter array to be written to the character stream
  • Return type: This method does not return any value
  • Exception: Throws IOException if an I/O error occurs during writing.

5. write(char[] carray, int offset, int maxlen)

It writes some part of the character array to the character stream. 

Syntax:

public abstract void write(char[] carray, int offset, int maxlen)

Parameter:

  1. carray: character to be written to the character stream
  2. offset: start position of the character array
  3. maxlen: maximum no. of the character of the carray has to written

Return type: This method does not return any value

Exception: Throws IndexOutOfBoundsException if the offset or maxlen is invalid and IOException if an I/O error occurs.

6. close()

It closes the character stream, flushing it first. 

Syntax:

 public abstract void close()

  • Parameter: This method does not take any parameter.
  • Return type: This method does not return any value.
  • Exception: Throws IOException if an I/O error occurs during closing.

7. flush()

It flushes the Writer stream. Flushing one stream invocation will flush all other buffer in chain. 

Syntax:

public void flush()

  • Parameter: This method does not take any parameter.
  • Return type: This method does not return any value.
  • Exception: Throws IOException if an I/O error occurs while flushing.

8. append(char Sw)

It appends a single character to the Writer. 

Syntax:

public Writer append(char Sw)

  • Parameter: character to be append
  • Return type: This method return a writer.
  • Exception: Throws IOException if an I/O error occurs during appending.

9. append(CharSequence char_sq) 

It appends specified character sequence to the Writer. 

Syntax:

public Writer append(CharSequence char_sq)

  • Parameter: Character sequence to append.
  • Return type: This method return a writer, if char sequence is null, then NULL appends to the Writer.
  • Exception: throws IOException if an I/O error occurs..

10. append(CharSequence char_sq, int start, int end)

It appends specified part of a character sequence to the Writer. 

Syntax:

public Writer append(CharSequence char_sq, int start, int end)

Parameter:

  1. char_sq: Character sequence to append.
  2. start: start of character in the Char Sequence
  3. end: end of character in the Char Sequence

Return type: This method return a writer, if char sequence is null, then NULL appends to the Writer.

Exception: Throws IndexOutOfBoundsException if the start or end values are invalid and IOException if an I/O error occurs.

Example: The below Java program demonstrates how to append individual character, entire sequence and substrings to the writer.

Java
import java.io.*;

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

        // Writer to output to the console
        Writer w = new PrintWriter(System.out);

        System.out.println("Example 1: append(char)");

        // Appending Characters
        w.append('G');
        w.append('e');
        w.append('e');
        w.append('k');
        w.append('s');
        w.flush();

        System.out.println();

        System.out.println(
            "Example 2: append(CharSequence)");
        CharSequence t = "Hello, Geeks!";

        // Appending entire CharSequence
        w.append(t);
        w.flush();

        System.out.println();

        System.out.println(
            "Example 3: append(CharSequence, start, end)");

        // Appending substring "Hello"
        w.append(t, 0, 5);
        w.append(" ");

        // Appending substring "Geeks"
        w.append(t, 7, 12);
        w.flush();

        System.out.println();

        // Close the writer
        w.close();
    }
}

Output
Example 1: append(char)
Geeks
Example 2: append(CharSequence)
Hello, Geeks!
Example 3: append(CharSequence, start, end)
Hello Geeks

Article Tags :
Practice Tags :

Similar Reads