The BufferedReader
and InputStreamReader
classes are essential in Java for handling input from various sources, such as the command line, files, or network streams. When used together, they provide an efficient way to read text-based input while optimizing performance.
When reading input from the console (or any input stream), we often chain InputStreamReader and BufferedReader for efficiency and flexibility:
Key Points of Combined Usage:
BufferedReader
improves the performance of input operations by buffering the input, thus reducing the frequency of I/O operations.BufferedReader
provides methods like readLine()
for reading entire lines of text efficiently, making it easier to work with user input.InputStreamReader
ensures that the byte input (from System.in
or other input streams) is correctly converted into characters for text-based processing.import java.io.*;
public class Example3 {
public static void main(String[] args) throws IOException {
// Chaining BufferedReader and InputStreamReader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your age: ");
String age = br.readLine(); // Reads input from the user as a String
int ageInt = Integer.parseInt(age); // Convert input to an integer
System.out.println("You are " + ageInt + " years old.");
}
}
Definition:
InputStreamReader
is a bridge from byte streams to character streams. It reads bytes from an InputStream
and decodes them into characters using a specified character encoding (e.g., UTF-8).System.in
, files, or network streams.Constructor:
The most common constructor used with InputStreamReader
public InputStreamReader(InputStream in);
in: The input stream (like System.in for command-line input).
Key Points:
InputStreamReader
reads byte data from an InputStream
and converts it into characters using a specified encoding (or the default encoding if none is specified).xxxxxxxxxx
import java.io.*;
public class Example1 {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
System.out.println("Enter some text: ");
char[] input = new char[100]; // Buffer to hold input characters
isr.read(input); // Read input from the command line
System.out.println("You entered: " + new String(input));
}
}
In this example, InputStreamReader
converts the byte stream (coming from System.in
, which reads input from the command line) into character data. It reads the input into a character array and prints the result.
Definition:
BufferedReader
is a wrapper around a Reader
(such as InputStreamReader
) that reads text from a character-input stream efficiently. It buffers the input data, allowing for efficient reading of characters, arrays, and lines.Constructor:
The common constructor:
public BufferedReader(Reader in);
in: The reader that provides the character data (e.g., an InputStreamReader).
Key Points:
BufferedReader
adds buffering to the reading process, which improves performance by reducing the number of read operations.readLine()
, which reads an entire line of text at once.System.in
or file), reducing I/O operation overhead.xxxxxxxxxx
import java.io.*;
public class Example2 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your name: ");
String name = br.readLine(); // Reads an entire line of input
System.out.println("Hello, " + name + "!");
}
}
Here, BufferedReader
is wrapping the InputStreamReader
to read input from the console more efficiently. The readLine()
method reads an entire line of input from the user, making it more convenient than reading a character array.