Java BufferedWriter Class

Java BufferedWriter Class

In this tutorial, we will learn about Java BufferedWriter and its methods with the help of examples.

The BufferedWriter class of the java.io package can be used with other writers to write data (in characters) more efficiently.

It extends the abstract class Writer.

The BufferedWriter class is a subclass of Java Writer.


Working of BufferedWriter

The BufferedWriter maintains an internal buffer of 8192 characters.

During the write operation, the characters are written to the internal buffer instead of the disk. Once the buffer is filled or the writer is closed, the whole characters in the buffer are written to the disk.

Hence, the number of communication to the disk is reduced. This is why writing characters is faster using BufferedWriter.


Create a BufferedWriter

In order to create a BufferedWriter, we must import the java.io.BufferedWriter package first. Once we import the package here is how we can create 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 created a BufferedWriter named buffer with the FileWriter named file.

Here, the internal buffer of the BufferedWriter has the default size of 8192 characters. However, we can specify the size of the internal buffer as well.

// 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.


Methods of BufferedWriter

The BufferedWriter class provides implementations for different methods 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 created a buffered writer named output along with FileWriter. The buffered writer is linked 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() method.

Here when we run the program, the output.txt file is filled with the following content.

This is a line of text inside the file.

flush() Method

To clear the internal buffer, we can use the flush() method. This method forces the writer to write all data present in the buffer to the destination file.

For example, suppose we have an empty file 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.

When we run the program, the file output.txt is filled with the text represented by the string data.


close() Method

To close the buffered writer, we can use the close() method. Once the close() method is called, we cannot 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).