One of the numerous tools provided in Java's comprehensive standard library is the BufferedReader class, which is recognized for its adaptability and effectiveness in processing character input from diverse origins. The Java BufferedReader class is employed for reading text from a character-oriented input stream. It facilitates reading data line by line through the readLine method, enhancing performance speed. This class is a subclass of the Reader class.
Java BufferedReader Class Declaration
Within Java's java.io package, the BufferedReader class presents a practical solution for extracting text from an input stream. This class is an extension of the abstract Reader class. It introduces enhanced features for input buffering, which is beneficial in situations necessitating the retrieval of character-based information, like parsing textual documents or managing network interactions.
Now, let's examine the declaration of the Java.io.BufferedReader class:
public class BufferedReader extends Reader
Java BufferedReader Class Constructors
| Constructor | Description |
|---|---|
| BufferedReader(Reader rd) | It is used to create a buffered character input stream that uses the default size for an input buffer. |
| BufferedReader(Reader rd, int size) | It is used to create a buffered character input stream that uses the specified size for an input buffer. |
Java BufferedReader Class Methods
| Method | Description |
|---|---|
| int read() | It is used for reading a single character. |
| int read(char[] cbuf, int off, int len) | It is used for reading characters into a portion of anarray. |
| boolean markSupported() | It is used to test the input stream support for the mark and reset method. |
| String readLine() | It is used for reading a line of text. |
| boolean ready() | It is used to test whether the input stream is ready to be read. |
| long skip(long n) | It is used for skipping the characters. |
| void reset() | It repositions thestreamat a position the mark method was last called on this input stream. |
| void mark(int readAheadLimit) | It is used for marking the present position in a stream. |
| void close() | It closes the input stream and releases any of the system resources associated with the stream. |
Java BufferedReader Example
In this instance, we are retrieving information from the text file named testout.txt by utilizing the Java BufferedReader class.
BufferedReaderExample.java
package com.example;
import java.io.*;
public class BufferedReaderExample {
public static void main(String args[])throws Exception{
FileReader fr=new FileReader("D:\\testout.txt");
BufferedReader br=new BufferedReader(fr);
int i;
while((i=br.read())!=-1){
System.out.print((char)i);
}
br.close();
fr.close();
}
}
In this scenario, we are considering that the data is stored in a file named "testout.txt":
Output:
Welcome to Java.
Closing the BufferedReader
It is crucial to remember that upon completing the use of a BufferedReader, it is necessary to close it to free up any system resources linked to it, like file handles or network connections. In the provided instance, a try-with-resources statement is employed to guarantee the automatic closure of the BufferedReader when it is no longer required.
Reading data from console by InputStreamReader and BufferedReader
In this instance, we are establishing a connection between the BufferedReader stream and the InputStreamReader stream to read data line by line from the keyboard.
BufferedReaderExample.java
package com.example;
import java.io.*;
public class BufferedReaderExample{
public static void main(String args[])throws Exception{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
System.out.println("Enter your name");
String name=br.readLine();
System.out.println("Welcome "+name);
}
}
Output:
Enter your name
Nakul Jain
Welcome Nakul Jain
An additional illustration of how to continuously read input from the console until the user enters the command "stop".
In this instance, we are continuously reading and displaying information until the user enters the command "stop".
BufferedReaderExample.java
package com.example;
import java.io.*;
public class BufferedReaderExample{
public static void main(String args[])throws Exception{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
String name="";
while(!name.equals("stop")){
System.out.println("Enter data: ");
name=br.readLine();
System.out.println("data is: "+name);
}
br.close();
r.close();
}
}
Output:
Enter data: Nakul
data is: Nakul
Enter data: 12
data is: 12
Enter data: stop
data is: stop
Java's BufferedReader class is a robust and efficient tool for reading character input from diverse origins. It enhances input handling by buffering data and providing user-friendly functions for text reading. This simplifies the management of input streams, making it a valuable asset for a wide range of activities such as file operations and network interactions. Whether you are dealing with extensive text files or communicating with external data repositories, BufferedReader equips Java programmers with the necessary utilities to effectively manage input streams within their software.