Understanding BufferedWriter in Java with Examples

  • BufferedWriter class is typically used to write character data to a target character output stream (such as a file or network connection) more efficiently.
  • When you use BufferedWriter to write, the data gets written to an internal buffer.
  • Only once the internal buffer is full OR if there is a need to flush, this data gets written to the target character stream/writer.
  • This buffering approach reduces the costly interactions with writing to the network or the disk thereby improving the performance compared to using non buffered writers.

BufferedWriter is part of JDK’s standard library. It is included as part of the java.io package. It has internal buffer of more than 8000 characters.

Basic usage of BufferedWriter

Getting ready to instantiate BufferedWriter

Since BufferedWriter class resides in java.io package, we must import the java.io package first.

Instantiating BufferedWriter

BufferedWriter class has two main constructors as shown below.

  • BufferedWriter(Writer writer)
  • BufferedWriter(Writer writer, int size)

Notice that we need an underlying Writer instance that our BufferredWriter will wrap.

Let’s create a FileWriter instance. FileWriter allows us to write data to files.

FileWriter fw = new FileWriter("D:\\BufferedWriter\\output.txt");

The above statement will throw an IOException if the file is not found. Therefore, let’s add a try-catch block.

    try{
      FileWriter fw = new FileWriter("D:\\BufferedWriter\\output.txt");
    }catch(IOException e){
      e.getStackTrace();
    }

Now, let’s create a BufferedWriter instance wrapping our FileWriter fw.

 BufferedWriter bufferedWriter = new BufferedWriter(fw); 

Writing data using BufferedWriter

We’ll use the Writer’s write(String s) method to write character data to our file.

bufferedWriter.write("These characters written by Buffered Writer");

Always close the bufferedWriter once you’re done writing.

bufferedWriter.write("This string is being written using a BufferedWriter");
bufferedWriter.close();

Our full program –

import java.io.BufferedWriter;
import java.io.FileWriter;

public class Talentify {
  public static void main(String[] args){
    try{
      FileWriter fw = new FileWriter("D:\\BufferedWriter\\output.txt");
      BufferedWriter bufferedWriter = new BufferedWriter(fw);
      bufferedWriter.write("This string is being written using a BufferedWriter");

    }catch(IOException e){
      e.getStackTrace();
    }
  }
}

Let’s run our java program and check the contents of the output.txt file. It should say –

This string is being written using a BufferedWriter

Other considerations with BufferedWriter

Using Custom Internal Buffer Size

As we saw above, one of the BufferedWriter constructors allows us to specify the internal buffer size without going with the default 8192 character size.
The below example shows that to instantiate a BufferedWriter with custom (500) character length internal buffer.

...
      FileWriter fw = new FileWriter("D:\\BufferedWriter\\output.txt");
      BufferedWriter bufferedWriter = new BufferedWriter(fw, 500);
...

flush() method

The normal behavior of the BufferedWriter is to write all the characters when the buffer is full or the write operation has finished.
Using the flush() method, we can force the writer to write all data in the buffer to the underlying character output stream/writer.

import java.io.BufferedWriter;
import java.io.FileWriter;

public class Talentify {
  public static void main(String[] args){
    try{
      FileWriter fw = new FileWriter("D:\\BufferedWriter\\output.txt");
      BufferedWriter bufferedWriter = new BufferedWriter(fw, 500);
      bufferedWriter.write("This string is being written using a BufferedWriter with an internal buffer length of 500");
      bufferedWriter.flush(); // flushes the buffer
      bufferedWriter.close();
    }catch(Exception e){
      e.getStackTrace();
    }
  }
}

close() method

close() method is used to release the resources attached to the stream.
close() also makes sure that any data still in the buffer is flushed before releasing the resources. Since Java 7, you can use try-with statements which will automatically close the BufferedWriter stream.
If you have already closed the stream, an invocation of write() or flush() methods will result in an IOException.

The below code will throw an exception. Since the stream has been closed, you can’t call the write() method.

public class Talentify {
  public static void main(String[] args){
    try{
      FileWriter fw = new FileWriter("D:\\BufferedWriter\\output.txt");
      BufferedWriter bufferedWriter = new BufferedWriter(fw, 500);
      bufferedWriter.write("This string is being written using a BufferedWriter  with an internal buffer length of 500");
      bufferedWriter.close();
      bufferedWriter.write("Trying to write more characters after closing the stream"); // throws IOException
    }catch(IOException e){
      e.getStackTrace();
    }
  }
}

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.