Programs very often include a requirement to quickly write data to a file or elsewhere. And this raises questions: How should we do this? Which class should we choose? Today we'll get to know a suitable candidate for this role — the BufferedWriter class.

Why do we need BufferedWriter?

BufferedWriter is a class that writes buffered characters to a stream. It lets you reduce the number of times you access physical media. That is, instead of writing a single character each time, it writes data to a buffer, and then writes all the characters to the medium all at once.

This greatly increases write speeds. The default buffer size is 8192 characters, but this can be changed by specifying a new size in the constructor:


BufferedWriter(Writer in, int sz)

Here, the constructor's first argument is a stream that will receive the data we write. And it turns out that sz is the size of the new buffer.

Java also has a BufferedReader class: it is used for buffered reading of data.

What exactly is a buffer? Let's take a real life example. A buffer is like a basket or shopping cart at a supermarket. Instead of walking up to the checkout with a single item, paying for it, putting it in the trunk of your car, and then coming back for another item, we can take a shopping cart, put everything we want in it, and then pay at the checkout. This is exactly how a buffer works: it collect data and then takes everything and writes it, instead of writing each fragment separately.

Constructors and methods of the BufferedWriter class

Now let's take a closer look at the BufferedWriter class. There are two constructors for creating an object:


public BufferedWriter(Writer out) 
public BufferedWriter(Writer out, int sz)

Where out in both constructors is the stream to write to, and sz, as already mentioned, is the size of the buffer.

The BufferedWriter class also has several methods. We'll take a closer look at some of them today:

write(char[] array) Writes a char array to the buffer
write(String s, int off, int len) Writes part of a string to the buffer
append(char c) Write a character to the buffer
append(CharSequence csq, int start, int end) Writes part of an array to the buffer
newLine() Writes a line separator
flush() Flushes the stream

Let's write a program that will write a value to a file. For the Writer parameter, we'll pass a FileWriter to the constructor. It is used to write text files and has several constructors to initialize objects:

FileWriter(File file)
FileWriter(File file, boolean append)
FileWriter(FileDescriptor fd)
FileWriter(String fileName)
FileWriter(String fileName, boolean append)

For our example, we'll use a constructor that takes a filename:


try(BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("file.txt"))){
	
	String message = "Hello, Amigo! This is a very important message!";
	bufferedWriter.write(message);
	bufferedWritter.flush();
}
catch(IOException ex){
System.out.println(ex.getMessage());
 }

Our code will use the write(String str) method to write str to the file.txt file.

There are other methods for writing:

  • write(char[] array) — this variant accepts and writes a char array;

  • write(String s, int off, int len) — this variant takes a string s; an offset off, which is index of the character to start writing from; and len, which is the length of the string (substring) to be written.


try(BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("file.txt"))){
	String message = "Hello, Amigo! This is a very important message!";
	bufferedWriter.write(message, 0, 11);
 	bufferedWriter.flush();

} catch(IOException ex) {
System.out.println(ex.getMessage());
}

This code will write "Hello, Amig" to the file, since we told the method to write 11 characters starting from index 0.

Our code also has a try-with resources block:


try(BufferedWriter bufferedWritter = new BufferedWriter(new FileWriter("file.txt"))) 

This means that the close() method is called automatically on my bufferedWriter object, because it implements the AutoCloseable interface.

The flush() method in the code is used to flush the output stream, forcing all buffered bytes to be written. The write may not occur without this call, since it is what indicates that the buffer must be flushed and that the buffered bytes must be written.

The BufferedWriter class also has a newLine() method that adds a new line to our message:


try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("file.txt"))) {
        String message = "Hello, Amigo! This is a very important message!";
        bufferedWriter.write(message, 0, 13);
        bufferedWriter.newLine();
        bufferedWriter.write(message, 15, 33);
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
    }

In the file, we get:

Hello, Amigo!
This is a very important message!

The signature of the append() method looks like this:


public Writer append(CharSequence csq, int start, int end)

It is used to add csq. Here start is the index of the first character, and end is the index of the last character of the string (or substring) to be inserted. The character with index end is not inserted.


try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("file.txt"))) {
    String message = "Hello, Amigo! This is a very important message!";
    bufferedWriter.append(message, 0, 7);
    bufferedWriter.flush();
} catch (IOException ex) {
    System.out.println(ex.getMessage());
}

This code will give you:

Hello,

That is, with the append method, you specify which part of the string to add to the buffer.

Looking more closely at the difference between write() and append(), we first see that they both do the same thing in principle — they write values.

However, the difference is that the append method is newer and takes a CharSequence as an argument, and because String implements CharSequence, we can pass Strings and StringBuilders, and StringBuffers to the append method. But the write() method will only accept a String.

That's it for now! Today we got acquainted with buffers, how to perform buffered writing to a file, as well as the methods that you can use to do this.