In this tutorial, we will learn about Java BufferedReader and its strategies with the help of examples.
The BufferedReader class of the java.io package can be used with different readers to understand data (in characters) all the more proficiently.
It extends the abstract class Reader.
In this article, you will learn-
Working of BufferedReader
The BufferedReader keeps an inward buffer of 8192 characters.
During the read activity in BufferedReader, a chunk of characters is read from the disk and stored in the inside buffer. What’s more, from the inside buffer characters are read exclusively.
Thus, the number of correspondence to the disk is decreased. This is the reason reading characters is quicker using BufferedReader.
Make a BufferedReader
To make a BufferedReader, we should import the java.io.BuferedReader package first. When we import the package, here is the way we can make the reader.
// Creates a FileReader
FileReader file = new FileReader(String file);
// Creates a BufferedReader
BufferedReader buffer = new BufferedReader(file);
In the above example, we have made a BufferedReader named buffer with the FileReader named file.
Here, the inward buffer of the BufferedReader has the default size of 8192 characters. In any case, we can indicate the size of the inward buffer too.
// Creates a BufferdReader with specified size internal buffer
BufferedReader buffer = new BufferedReader(file, int size);
The buffer will help to read characters from the files more quickly.
Methods of BufferedReader
The BufferedReader class provides implementations for different methods present in Reader.
read() Method
- read() – reads a single character from the internal buffer of the reader
- read(char[] array) – reads the characters from the reader and stores in the specified array
- read(char[] array, int start, int length) – reads the number of characters equal to length from the reader and stores in the specified array starting from the position start
For example, suppose we have a file named input.txt with the following content.
This is a line of text inside the file.
Let’s try to read the file using BufferedReader.
import java.io.FileReader;
import java.io.BufferedReader;
class Main {
public static void main(String[] args) {
// Creates an array of character
char[] array = new char[100];
try {
// Creates a FileReader
FileReader file = new FileReader("input.txt");
// Creates a BufferedReader
BufferedReader input = new BufferedReader(file);
// Reads characters
input.read(array);
System.out.println("Data in the file: ");
System.out.println(array);
// Closes the reader
input.close();
}
catch(Exception e) {
e.getStackTrace();
}
}
}
Output
Data in the file:
This is a line of text inside the file.
In the above example, we have made a buffered reader named input. The buffered reader is connected with the input.txt record.
FileReader file = new FileReader("input.txt");
BufferedReader input = new BufferedReader(file);
Here, we have used the read() strategy to read an array of characters from the inner buffer of the buffered reader.
skip() Method
To discard of and skip the predefined number of characters, we can use the skip() technique. For instance,
import java.io.FileReader;
import java.io.BufferedReader;
public class Main {
public static void main(String args[]) {
// Creates an array of characters
char[] array = new char[100];
try {
// Suppose, the input.txt file contains the following text
// This is a line of text inside the file.
FileReader file = new FileReader("input.txt");
// Creates a BufferedReader
BufferedReader input = new BufferedReader(file);
// Skips the 5 characters
input.skip(5);
// Reads the characters
input.read(array);
System.out.println("Data after skipping 5 characters:");
System.out.println(array);
// closes the reader
input.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}
Output
Data after skipping 5 characters:
is a line of text inside the file.
In the above example, we have used the skip() strategy to skip 5 characters from the record reader. Subsequently, the characters ‘T’, ‘h’, ‘I’, ‘s’ and ‘ are skipped from the original file.
close() Method
To close the buffered reader, we can use the close() strategy. When the close() technique is called, we can’t use the reader to read the data.
Other Methods of BufferedReader
Method | Description |
ready() | checks if the file reader is ready to be read |
mark() | mark the position in reader up to which data has been read |
reset() | returns the control to the point in the reader where the mark was set |
To learn more, visit Java BufferedReader (official Java documentation).
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.
i like this excellent article