Last Updated : 23 Jul, 2025
Java writer class is an abstract class in the java.io package. It is designed for writing character streams. Writer class in Java 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 FileWriter, BufferedWriter, or PrintWriter.
Example 1: The below Java program demonstrates the working of the FileWriter class to write data to a text file.
Java
// Demonstrate the woking of FileWriter class
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(
"Geeks for Geeks");
}
catch (IOException e) {
System.out.println("An error occurred: "
+ e.getMessage());
}
}
}
Declaration of the Writer Class
Constructors of the Writer Classpublic abstract class Writer extends Object implements Appendable, Closeable, Flushable
The Writer
class in Java has two protected constructors that allow for the creation of character streams with synchronization capabilities.
Writer():
Creates a new character stream that can itself synchronize on the writer.There are certain methods associated with the Java Writer class as mentioned below:
1. write(int char)Syntax:
public void write(int char)
It writes a string to the character stream.
Syntax:
public void write(String str)
It writes some part of the string to the character stream.
Syntax:
public void write(String str, int offset, int maxlen)
It writes character array to the character stream.
Syntax:
public void write(char[] carray)
It writes some part of the character array to the character stream.
Syntax:
public abstract void write(char[] carray, int offset, int maxlen)
It closes the character stream, flushing it first.
Syntax:
public abstract void close()
It flushes the Writer stream. Flushing one stream invocation will flush all other buffer in chain.
Syntax:
public void flush()
It appends a single character to the Writer.
Syntax:
public Writer append(char Sw)
It appends specified character sequence to the Writer.
Syntax:
public Writer append(CharSequence char_sq)
It appends specified part of a character sequence to the Writer.
Syntax:
public Writer append(CharSequence char_sq, int start, int end)
Example 2: The below Java program demonstrates how to append individual character, entire sequence and substrings to the writer.
Java
// Java Program to demonstrates appending
// Characters and String to the writer
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();
}
}
Example 1: append(char) Geeks Example 2: append(CharSequence) Hello, Geeks! Example 3: append(CharSequence, start, end) Hello Geeks
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4