Java

BufferedReader and BufferedWriter

In Java programming, there can be situations where the developer needs to work with complex entries efficiently. For instance, fetching long strings from a “stream”, especially in the case of bulk data. In such cases, “BufferedReader” and “BufferedWriter” in Java are of great aid in increasing the coding efficiency by reducing the “I/O” operations of the disk.

This article will elaborate on the functionalities of “BufferedReader” and “BufferedWriter” in Java.

What are “BufferedReader” and “BufferedWriter” in Java?

The “BufferedReader” reads from the “character-based stream” and the “BufferedWriter” writes to it (character-based stream). These approaches enable the programmer to effectively read/write characters, arrays, strings, etc. These functionalities work in such a way that the data is contained in a buffer and then written to the file when the buffer becomes full.

Before proceeding to the demonstration, make sure to include the following libraries to read, and write text from a “character-input stream” and enable Java “I/O(Input/Output)” operations:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

Demonstration: “BufferedReader” and “BufferedWriter” in Java

In this demonstration, we will discuss the following two operations:

Writing to a File Using “BuffererdWriter” in Java

First of all, write the content to a file via “BufferedWriter”. This can be achieved/done by using the “Paths.get()” and “write()” methods. The former method transforms a sequence of strings to a “Path instance” and the latter method writes text to the file.

Syntax

public static Paths get(String x, String y)

In this syntax:

  • String x” refers to the initial part of the path.
  • String y” corresponds to the extra strings that must be appended to the path.

Let’s move on to the following code snippet:

private static void writeContent() throws IOException {
 Path writePath = Paths.get("bufferFile.txt");
 try (BufferedWriter writeContent = Files.newBufferedWriter(writePath)) {
  writeContent.write("Harry");
  writeContent.write("\nJohn");
  writeContent.write("\nDavid");
}
 System.out.println("The content is written to the file via BufferedWriter!");
}

In this code block, perform the following steps:

  • Firstly, define a function named “writeContent()” and declare the “IOException” via the “throws” keyword.
  • In its definition, apply the “Paths.get()” method to create a path instance by referring to the stated text file.
  • In the next step, apply the “Files.newBufferedWriter()” method by referring to the file that needs to be written.
  • After that, associate the “write()” method for writing the specified string content in the file.

Reading a File Using “BufferedReader” in Java

In the following snippet of code, the written file can be read via “BufferedReader”:

private static void readContent() throws IOException {
 Path readPath = Paths.get("bufferFile.txt");
 try (BufferedReader writeContent = Files.newBufferedReader(readPath)) {
  String cont;
  System.out.println("\nReading File via BufferedReader...");
 while ((cont = writeContent.readLine()) != null) {
  System.out.println(cont);
}
}}

In these lines of code:

  • Declare the function named “readContent()”.
  • In its definition, point to the written file and open it for reading using the “Files.newBufferedReader()” method.
  • Now, check for the condition such that the file is not “null” via the “readLine()” method that checks the file line by line.
  • Upon the satisfied condition, log the file contents on the console.

Now, go through the below “main()” method:

public static void main(String[] args) throws IOException {
 writeContent();
 readContent();
}

Here, simply invoke the defined functions to write and read the file contents, respectively.

Output

In the above output, it is evident that the content is written and read from the file appropriately.

Write and Read File

In this outcome, it can be seen that the file is written, and accessed properly.

Entire Code

public class bufferreaderwriter {
 private static void writeContent() throws IOException {
  Path writePath = Paths.get("bufferFile.txt");
  try (BufferedWriter writeContent = Files.newBufferedWriter(writePath)) {
   writeContent.write("Harry");
   writeContent.write("\nJohn");
   writeContent.write("\nDavid");
}
  System.out.println("The content is written to the file via BufferedWriter!");
}
 private static void readContent() throws IOException {
  Path readPath = Paths.get("bufferFile.txt");
  try (BufferedReader writeContent = Files.newBufferedReader(readPath)) {
  String cont;
  System.out.println("\nReading File via BufferedReader...");
  while ((cont = writeContent.readLine()) != null) {
   System.out.println(cont);
}
}}
 public static void main(String[] args) throws IOException {
  writeContent();
  readContent();
}}

This was all about “BufferReader” and “BufferWriter” in Java.

Conclusion

In Java, the “BufferedReader” reads from the character-based stream, and the “BufferedWriter” writes to it (character-based stream). These approaches buffer the characters to efficiently read and write characters, arrays, and strings, respectively, etc. This blog discussed the implementation of “BufferedReader” and “BuffererdWriter” in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.