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.
WriterDeclaration 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 FileWriter, BufferedWriter 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());
}
}
}
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();
}
}
}
OutputData written successfully.
Output File: example.txt
outputExplanation: 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();
}
}
}
OutputData written using BufferedWriter.
Output File: buffered.txt
outputExplanation: 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();
}
}
}
OutputData appended successfully.
Output File: example.txt
OutputExplanation: 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:
What does the Writer class in Java primarily handle?
-
Reading binary data from a file
-
Writing text data to a file
-
Writing binary data to a file
-
Reading text data from a file
Which of the following is a subclass of Writer in Java?
Why can’t we create an object of the Writer class directly?
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 >
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java