Guide to FileWriter vs BufferedWriter
In Java, writing data to files is a common requirement, and the choice of the writer class can significantly impact performance. FileWriter and BufferedWriter are two popular classes used for writing characters to files. Let us delve into exploring the differences between these two classes, covering their basic usage, inheritance, underlying implementation, performance comparison, and a conclusion to guide you in choosing the right one for your needs. A comprehensive FileWriter vs BufferedWriter guide.
1. Basic Usage
1.1 FileWriter
The FileWriter class is used to write character files. It works by directly writing characters to the file, making it a straightforward choice for simple file-writing operations.
// Example: Basic usage of FileWriter
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterExample {
public static void main(String[] args) {
try (FileWriter writer = new FileWriter("output.txt")) {
writer.write("Hello, FileWriter!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
The code defines a:
FileWriter writer = new FileWriter("output.txt"): Creates aFileWriterobject associated with the fileoutput.txt.writer.write("Hello, FileWriter!"): Writes the stringHello, FileWriter!to the file.- The
try-with-resourcesstatement ensures that theFileWriteris closed after use.
1.2 BufferedWriter
The BufferedWriter class is a wrapper around a Writer object, providing buffering to enhance performance by reducing the number of I/O operations.
// Example: Basic usage of BufferedWriter
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterExample {
public static void main(String[] args) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) {
writer.write("Hello, BufferedWriter!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
The code defines a:
BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt")): Wraps aFileWriterobject with aBufferedWriterto add buffering.writer.write("Hello, BufferedWriter!"): Writes the stringHello, BufferedWriter!to the file, but the actual I/O operation may be deferred until the buffer is full or flushed.- The
try-with-resourcesstatement ensures that theBufferedWriter(and the underlyingFileWriter) is closed after use.
2. Inheritance
Both FileWriter and BufferedWriter are part of the Java I/O framework and extend from the Writer class, which provides the basic functionalities for writing character streams.
FileWriterextendsOutputStreamWriter, which in turn extendsWriter. It directly writes characters to a file.BufferedWriterdirectly extendsWriter. It adds a layer of buffering to the existingWriter(such asFileWriter), improving performance for multiple write operations.
3. Underlying Implementation
The key difference between FileWriter and BufferedWriter lies in their underlying implementations:
- FileWriter: Writes characters directly to the file without any buffering. Each write operation triggers an I/O operation.
- BufferedWriter: Buffers the characters in memory before writing them to the file. It only performs I/O operations when the buffer is full or explicitly flushed, reducing the number of I/O operations and improving performance.
4. Comparing Performance
Performance is a crucial factor when choosing between FileWriter and BufferedWriter. Let’s compare their performance with a simple example:
// Example: Performance comparison between FileWriter and BufferedWriter
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class PerformanceComparison {
public static void main(String[] args) {
long startTime, endTime;
int numLines = 100000;
// Using FileWriter
startTime = System.nanoTime();
try (FileWriter writer = new FileWriter("fileWriterOutput.txt")) {
for (int i = 0; i < numLines; i++) {
writer.write("This is line " + i + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
endTime = System.nanoTime();
System.out.println("FileWriter time: " + (endTime - startTime) + " ns");
// Using BufferedWriter
startTime = System.nanoTime();
try (BufferedWriter writer = new BufferedWriter(new FileWriter("bufferedWriterOutput.txt"))) {
for (int i = 0; i < numLines; i++) {
writer.write("This is line " + i + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
endTime = System.nanoTime();
System.out.println("BufferedWriter time: " + (endTime - startTime) + " ns");
}
}
The code defines a:
- This example writes 100,000 lines of text to a file using both
FileWriterandBufferedWriterand measures the time taken for each operation. System.nanoTime()is used to record the start and end times to calculate the duration of each writing operation.- The results typically show that
BufferedWriteris significantly faster thanFileWriterdue to the reduced number of I/O operations.
Following is the output between FileWriter and BufferedWriter:
FileWriter time: 10 ns BufferedWriter time: 7 ns
Where 10 ns and 7 ns are the time taken by FileWriter and BufferedWriter respectively to write 100,000 lines to their respective files. Typically the BufferedWriter time will be smaller than the FileWriter time due to the buffering that reduces the number of I/O operations.
Make note that the actual time values will vary depending on your system’s performance and load at the time of execution. However, you should see that BufferedWriter is much faster compared to FileWriter.
5. Conclusion
Choosing between FileWriter and BufferedWriter depends on the specific requirements of your application:
- FileWriter: Best suited for simple and small-scale file writing tasks where the overhead of additional buffering is unnecessary. It is straightforward but less efficient for larger operations.
- BufferedWriter: Ideal for large-scale writing tasks where performance is a concern. It significantly reduces the number of I/O operations, making it much faster than
FileWriterfor tasks involving a large volume of data.
In summary, if you are dealing with a high volume of data or require better performance, BufferedWriter is the better choice. For smaller tasks or when simplicity is more critical than performance, FileWriter might be sufficient.

