A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/java-io-writer-class-java/ below:

Java Writer Class - GeeksforGeeks

Java Writer Class

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 FileWriterBufferedWriter, 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

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) 

Syntax:

public void write(int char)

2. write(String str)

It writes a string to the character stream. 

Syntax:

public void write(String str)

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)

4. write(char[] carray) 

It writes character array to the character stream. 

Syntax:

 public void write(char[] carray)

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)

6. close()

It closes the character stream, flushing it first. 

Syntax:

 public abstract void close()

7. flush()

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

Syntax:

public void flush()

8. append(char Sw)

It appends a single character to the Writer. 

Syntax:

public Writer append(char Sw)

9. append(CharSequence char_sq) 

It appends specified character sequence to the Writer. 

Syntax:

public Writer append(CharSequence char_sq)

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)

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();
    }
}

Output
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