In this tutorial, we will learn about Java BufferedWriter and its strategies with the help of examples.
The BufferedWriter class of the java.io package can be used with different writers to write data (in characters) all the more proficiently.
It extends the abstract class Writer.
In this article, you will learn-
Working of BufferedWriter
The BufferedWriter keeps an inner buffer of 8192 characters.
During the writing activity, the characters are kept in touch with the inside buffer rather than the plate. When the buffer is filled or the writer is closed, the entire characters in the buffer are kept in touch with the disk.
Consequently, the quantity of correspondence to the disk is decreased. This is the reason writing characters is quicker using BufferedWriter.
Make a BufferedWriter
To make a BufferedWriter, we should import the java.io.BufferedWriter package first. When we import the package here is the manner by which we can make the buffered writer.
// Creates a FileWriter
FileWriter file = new FileWriter(String name);
// Creates a BufferedWriter
BufferedWriter buffer = new BufferedWriter(file);
In the above example, we have made a BufferedWriter named buffer with the FileWriter named file.
Here, the inner buffer of the BufferedWriter has the default size of 8192 characters. However, we can determine the size of the inner buffer also.
// Creates a BufferedWriter with specified size internal buffer
BufferedWriter buffer = new BufferedWriter(file, int size);
The buffer will help to write characters to the files more efficiently.
Strategies for BufferedWriter
The BufferedWriter class gives executions to various techniques present in Writer
write() Method
- write() – writes a single character to the internal buffer of the writer
- write(char[] array) – writes the characters from the specified array to the writer
- write(String data) – writes the specified string to the writer
Example: BufferedWriter to write data to a File
import java.io.FileWriter;
import java.io.BufferedWriter;
public class Main {
public static void main(String args[]) {
String data = "This is the data in the output file";
try {
// Creates a FileWriter
FileWriter file = new FileWriter("output.txt");
// Creates a BufferedWriter
BufferedWriter output = new BufferedWriter(file);
// Writes the string to the file
output.write(data);
// Closes the writer
output.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}
In the above example, we have made a buffered writer named output alongside FileWriter. The buffered writer is connected with the output.txt file.
FileWriter file = new FileWriter("output.txt");
BufferedWriter output = new BufferedWriter(file);
To write data to the file, we have used the write() technique.
Here when we run the program, the output.txt record is loaded up with the accompanying substance.
This is a line of text inside the file.
flush() Method
To clear the internal buffer, we can use the flush() technique. This technique forces the writer to write all data present in the buffer to the objective record.
For instance, assume we have a vacant document named output.txt.
import java.io.FileWriter;
import java.io.BufferedWriter;
public class Main {
public static void main(String[] args) {
String data = "This is a demo of the flush method";
try {
// Creates a FileWriter
FileWriter file = new FileWriter(" flush.txt");
// Creates a BufferedWriter
BufferedWriter output = new BufferedWriter(file);
// Writes data to the file
output.write(data);
// Flushes data to the destination
output.flush();
System.out.println("Data is flushed to the file.");
output.close();
}
catch(Exception e) {
e.getStackTrace();
}
}
}
Output
Data is flushed to the file.
At the point when we run the program, the record output.txt is loaded up with the content represented by the string data.
close() Method
To close the buffered writer, we can use the close() technique. When the close() strategy is called, we can’t use the writer to write the data.
Other Methods of BufferedWriter
Method | Description |
newLine() | inserts a new line to the writer |
append() | inserts the specified character to the current writer |
To learn more, visit Java BufferedWriter (official Java documentation).
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.