In this document, we will explore Java's Writer class, understand its uses, and look at some of its subclasses with practical examples.
In Java, the Writer class is an abstract class that provides a way to write character streams. The Writer class and its subclasses, such as FileWriter, BufferedWriter, and PrintWriter, are essential for writing text data to various sources. It serves as the superclass for all classes representing an output stream of characters. Let's dive into these classes, understand their functionalities, and see some examples.
Uses of Writer:
Writer is used for:
Sending text data over the network. Writing text data to other I/O sources like pipes. Subclasses of Writer:
In order to use the functionality of the Writer, we can use its subclasses. Some of them are:
How to create a Writer?
In order to create a Writer, we must import the java.io.Writer package first. Once we import the package, here is how we can create the writer.
// Creates a Writer
Writer output = new FileWriter();
Here, we have created a writer named output using the FileWriter class. It is because the Writer is an abstract class. Hence we cannot create an object of Writer.
Note: We can also create writers from other subclasses of the Writer class.
Java FileWriter
The FileWriter class is a subclass of Writer that writes characters to a file. It is simple to use and allows writing both text and character data to a file.
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterDemo {
public static void main(String[] args) {
FileWriter writer = null;
try {
writer = new FileWriter("output.txt");
writer.write("Hello, World!\n");
} catch (IOException e) {
System.out.println("An error occurred while writing to the file.");
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
System.out.println("Failed to close the writer.");
e.printStackTrace();
}
}
}
}
}
In the following example, we use the FileWriter class together with its write() method to write some text to the file output.txt we created in the example above. Note that when you are done writing to the file, you should close it with the close() method.
BufferedWriter
BufferedWriter is used to improve performance by buffering the data. Here’s how it works:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterDemo {
public static void main(String[] args) {
String data = "Buffered Hello, World!";
try (BufferedWriter writer = new BufferedWriter(new FileWriter("buffered_output.txt"))) {
writer.write(data);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Explanation:
We wrap FileWriter in a BufferedWriter. The write method writes the data to the buffer first, which then writes it to the file in bulk, improving performance. Other Writer Classes:
There are several other writer classes in Java, such as:
StringWriter: Used for writing to a string buffer, which can then be used to construct a string. PrintWriter
The PrintWriter class is a subclass of Writer that provides convenient methods to write formatted text. It can be used to write primitive data types and objects as text.
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class PrintWriterDemo {
public static void main(String[] args) {
PrintWriter writer = null;
try {
writer = new PrintWriter(new FileWriter("output.txt"));
writer.println("Hello, World!");
} catch (IOException e) {
System.out.println("An error occurred while writing to the file.");
e.printStackTrace();
} finally {
if (writer != null) {
writer.close();
}
}
}
}
Explanation:
We create a PrintWriter object to write to a file named output.txt. The println method writes different types of data to the file, each followed by a newline. Methods of Writer:
The Writer class provides different methods that are implemented by its subclasses. Here are some of the methods:
write(char[] array) - writes array of characters from the specified array to the output stream
Syntax :
public void write(char[] c) throws IOException
Parameters:
c - Array of characters to be written
Throws:
IOException - If an I/O error occurs
void write(String data) - writes the specified string to the writer
Syntax :
public void write(String str) throws IOException
Parameters:
str - String to be written
Throws:
IOException
append(char c) - Appends the specified character to this writer.An invocation of this method of the form out.append(c) behaves in exactly the same way as the invocation of out.write(c)
Syntax :
public Writer append(char c) throws IOException
Parameters:
c - The 16-bit character to append
Returns:
This writer
Throws:
IOException
flush() - Flushes the stream.If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.
Syntax :
public abstract void flush() throws IOException
Throws:
IOException