English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn about Java BufferedWriter and its methods with the help of examples.
The BufferedWriter class in the java.io package can be used with other writers to write data more efficiently (in terms of characters).
It inherits the abstract class Writer.
BufferedWriter maintains an internalof8192a character buffer.
During write operations, characters will be written to the internal buffer rather than the disk. Once the buffer is filled or the writer is closed, all characters in the buffer will be written to the disk.
This reduces the number of communications with the disk. This is why using BufferedWriter to write characters is faster.
To create a BufferedWriter, we must first import the java.io.BufferedWriter package. After importing the package, we can create a buffered writer.
//Create FileWriter FileWriter file = new FileWriter(String name); //Create BufferedWriter BufferedWriter buffer = new BufferedWriter(file);
In the above example, we created a BufferedWriter named buffer and a FileWriter named file.
Here, the default size of the internal buffer of BufferedWriter is8192characters. However, we can also specify the size of the internal buffer.
//Create a BufferedWriter with a specified internal buffer size BufferedWriter buffer = new BufferedWriter(file, int size);
The buffer will help to write characters to the file more efficiently.
BufferedWriter class provides different implementations of the methods existing in Writer.
write() - Write a single character to the internal buffer of the writer
write(char[] array) - Write the characters in the specified array to the writer
write(String data) - Write the specified string to the writer
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 { //Create FileWriter FileWriter file = new FileWriter("output.txt"); //Create BufferedWriter BufferedWriter output = new BufferedWriter(file); //Write a string to the file output.write(data); //Close writer output.close(); } catch (Exception e) { e.getStackTrace(); } } }
In the above example, we created a buffered writer and a file writer named output. The buffered writer is linked to the output.txt file.
FileWriter file = new FileWriter("output.txt"); BufferedWriter output = new BufferedWriter(file);
To write data to the file, we used the write() method.
Here, when we run the program,output.txtThe file will be filled with the following content.
This is a line of text inside the file.
To clear the internal buffer, we can use the flush() method. This method forces the writer to write all the data in the buffer to the target file.
For example, suppose we have a file namedoutput.txtan empty file.
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 { //Create FileWriter FileWriter file = new FileWriter("flush.txt"); //Create BufferedWriter BufferedWriter output = new BufferedWriter(file); //Write data to the file output.write(data); //Flush data to the target output.flush(); System.out.println("Data is flushed to the file."); output.close(); } catch(Exception e) { e.getStackTrace(); } } }
Output Results
data is flushed to the file.
When we run the program, the fileoutput.txtText represented by string data fills.
To close the buffered writer, we can use the close() method. Once the close() method is called, the writer cannot be used to write data.
Method | Description |
---|---|
newLine() | Insert a new line into the author |
append() | Insert the specified character into the current writer |