BufferedWriter Class in Java

The BufferedWriter class provides the essential functionality for efficiently writing character buffers into a file. It extends the Writer class, which serves as an abstract foundation for writing to character streams.

When utilizing the BufferedWriter, employing buffering can significantly enhance the performance of input and output operations. Instead of writing a single character at a time to the source, the BufferedWriter optimizes efficiency by writing a substantial amount of data in each operation. As a result, it typically exhibits noticeably improved speed, particularly when dealing with disk access and handling larger volumes of data.

Advantage

When you want to write strings there are two options. The BufferedWriter and the File Writer .

  1. If you want to write one string the File Writer is better.
  2. If you want to write multiple strings, the BufferedWriter is more efficient.

When utilizing the BufferedWriter, it offers the capability to buffer multiple strings together, allowing for improved efficiency. This advantage is further enhanced by the default buffer size, which is set to 8192 characters. By buffering multiple strings and taking advantage of the buffer size, the BufferedWriter can accomplish writing in just a single system call, minimizing overhead.

However, it is crucial to ensure that the BufferedWriter is properly cleared when invoked, particularly if there is any content remaining in the buffer. This step guarantees that all buffered data is appropriately handled and written to the desired destination.

How to use Java BufferedWriter

The following Java program writes an array into the external file using BufferedWriter.

import java.util.*; import java.io.*; public class TestClass{ public static void main(String[] args) { try { String weekdays[] = {"Monday", "Tuesday", "Wednsday", "Thursday", "Friday"}; File file = new File("D:/test.txt"); FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); for(int i=0;i<weekdays.length;i++){ bw.write(weekdays[i]); bw.newLine(); } bw.flush(); bw.close(); fw.close(); } catch (IOException e){ e.printStackTrace(); } } }

If you just want to print out the array like [a, b, c, ....], you can replace the loop with this one liner:

bw.write(Arrays.toString(weekdays));

How to append text to an existing file in Java?

The constructor FileWriter(file,true) append new content to the end of a file.

FileWriter fw = new FileWriter(file);

In the above code, all existing content will be overridden.

FileWriter fw = new FileWriter(file,true);

Above code keep the existing content and append the new content to the end of a file.

The following Java program append the new content to the end of a file.

import java.util.*; import java.io.*; public class TestClass{ public static void main(String[] args) { try { String weekdays[] = {"Saturday", "Sunday"}; File file = new File("D:/test.txt"); FileWriter fw = new FileWriter(file,true); BufferedWriter bw = new BufferedWriter(fw); for(int i=0;i<weekdays.length;i++){ bw.write(weekdays[i]); bw.newLine(); } bw.flush(); bw.close(); fw.close(); } catch (IOException e){ e.printStackTrace(); } } }

Conclusion

The BufferedWriter class in Java provides a way to write data efficiently to an output stream by using buffering. It optimizes write operations by reducing the number of calls to the native API, making it suitable for performance-critical tasks such as writing large volumes of data to files or streams.