BufferedReader and BufferedWriter Methods in Java 8 with examples

1. Introduction

In this article we will learn how to use newBufferedReader and newBufferedWriter method that was introduced in Files class in Java 8. There are several methods added in this class but will learn them in parts.

2. newBufferedReader using Path

Method signature

public static
BufferedReader newBufferedReader(Path path) throws IOException

This method opens a file and returns a BufferedReader to read text from file. We then read lines of file one by one using BufferedReader’s readLine method.

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

try (BufferedReader br = Files.newBufferedReader(
									Paths.get("words.txt"))) {
 
	String line;
	while ((line = br.readLine()) != null) {
		System.out.println(line);
	}
 
} catch (IOException io) {
	io.printStackTrace();
}

This method throws IOException if I/O error occurs while opening the file.

3. newBufferedReader using Path and Charset

Method signature

public static 
BufferedReader newBufferedReader(Path path, Charset cs) 
throws IOException

This method opens a file and returns a BufferedReader to read text from file.

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

try (BufferedReader br = Files.newBufferedReader(
									Paths.get("words.txt"), 
									StandardCharsets.UTF_8)) {
 
	String line;
	while ((line = br.readLine()) != null) {
		System.out.println(line);
	}
 
} catch (IOException io) {
	io.printStackTrace();
}

4. newBufferedWriter using Path and OpenOption

Method signature

public static 
BufferedWriter newBufferedWriter(Path path, OpenOption... options) 
throws IOException

This method opens or creates a file and returns the BufferedWriter to write text to the file. OpenOption is an object that specifies how to open or create a file. We are specifying StandardOpenOption enums CREATE_NEW and WRITE. The below example uses BufferedReader and BufferedWriter to read line from words.txt file and then write to new_words.txt file respectively.

import static java.nio.file.StandardOpenOption.CREATE_NEW;
import static java.nio.file.StandardOpenOption.WRITE;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
 
// BufferedReader and BufferedWriter are opened 
// in try-with-resources
try (BufferedReader br = Files.newBufferedReader(
									Paths.get("words.txt"));
	 BufferedWriter bw = Files.newBufferedWriter(
									Paths.get("new_words.txt"),
									CREATE_NEW, 
									WRITE)) {
 
	String line;
	while ((line = br.readLine()) != null) {
		bw.write(line + "\n");
	}
 
} catch (IOException io) {
	io.printStackTrace();
}

If we do not specify any OpenOptions then by default CREATE, TRUNCATE_EXISTING and WRITE options are present. In other words, it opens the file for writing, creating the file if it doesn’t exist, or initially truncating an existing file if it exists.

5. newBufferedWriter using Path, Charset and OpenOption

import static java.nio.file.StandardOpenOption.CREATE_NEW;
import static java.nio.file.StandardOpenOption.WRITE;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
 
// BufferedReader and BufferedWriter are opened 
// in try-with-resources
try (BufferedReader br = Files.newBufferedReader(
									Paths.get("words.txt"));
	 BufferedWriter bw = Files.newBufferedWriter(
									Paths.get("new_words.txt"),
									StandardCharsets.UTF_8, 
									CREATE_NEW, 
									WRITE)) {
 
	String line;
	while ((line = br.readLine()) != null) {
		bw.write(line + "\n");
	}
 
} catch (IOException io) {
    io.printStackTrace();
}

6. Conclusion

In this article, we saw two methods i.e. newBufferedReader and newBufferedWriter and how to read and write file with them respectively. In upcoming articles we will see additional methods of Files class.

Leave a Reply

Your email address will not be published. Required fields are marked *