Java Writer Class

Java Writer Class

What is Java Writer Class?

The Java Writer class is a subclass of the abstract class Writer, which provides a way to write characters to a file or another output stream.

The Writer class is a character stream class, which means it reads and writes Unicode characters, one at a time.The Writer class provides a set of methods that allow you to write characters to the stream

To understand the Java Writer Class, Read the Complete Article.

Sub Class of Writer:

The Java Writer class is an abstract class and cannot be instantiated. Instead, you can create instances of its subclasses to write characters to various types of output streams.

Here are some common subclasses of the Java Writer class:

  • FileWriter: Writes characters to a file. It inherits the methods of the Writer class and provides additional methods for working with files.
  • StringWriter: Writes characters to a string. It is useful for capturing output that would normally be written to the console or a file.
  • BufferedWriter: Adds buffering to an existing Writer instance, which can improve performance by reducing the number of write operations to the underlying stream.
  • CharArrayWriter: Writes characters to an in-memory buffer, which can be accessed as a character array.
  • PrintWriter: Provides methods for writing formatted text to an output stream.
  • OutputStreamWriter: Converts a byte-oriented OutputStream into a character-oriented Writer. This is useful for writing characters to streams that are designed to handle only bytes, such as network sockets or file streams.

Let’s look at the Java Writer Class Using FileWriter Method to perform certain operations.

Example 1: Java Program to Writer Using FileWriter Method

Run
import java.io.*;

public class Main
{
  public static void main (String[]args)
  {
    try
    {
      // Create a FileWriter object with the name of the file to write
      FileWriter writer = new FileWriter ("output.txt");

      // Write a string to the file
        writer.write ("Hello, World!");

      // Close the FileWriter object to release any resources held by it
        writer.close ();

        System.out.println ("Successfully wrote to the file.");
    } catch (IOException e)
    {
      System.out.println ("An error occurred.");
      e.printStackTrace ();
    }
  }
}

Output

Hello, World!

Example 2 : Java HashMap getOrDefault() Method

Run
import java.io.FileWriter;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {
        
        // Specify the file path and name
        String filePath = "example.txt";
        
        // Use try-with-resources to ensure the FileWriter is closed properly
        try (FileWriter writer = new FileWriter(filePath)) {
            
            // Write some data to the file
            writer.write("Hello, world!");
            writer.write(System.lineSeparator());
            writer.write("This is an example of using FileWriter to write data to a file.");
            
        } catch (IOException e) {
            System.err.println("An error occurred while writing to the file: " + e.getMessage());
        }
    }
} 

Output

Hello, world!
This is an example of using FileWriter to write data to a file.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription