// Java program to Accept input Using Buffered Reader Class in Java
import java.io.*;
class AcceptInput
{
public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String name = reader.readLine();
System.out.println("Hello : "+name);
}
}
The BufferedReader class in Java is used to read input from the user efficiently. It is commonly used for handling character input from the keyboard and reading text data quickly.
The BufferedReader class belongs to the java.io package and is often considered faster than some other input methods because it reads multiple characters at once.
This article explains how to accept input using the BufferedReader class in Java with syntax, examples, output, and explanation.
What is BufferedReader in Java?
BufferedReader is a predefined Java class used to read text input from the keyboard or files.
It reads data efficiently by buffering characters, arrays, and lines.
The BufferedReader class is mainly used when fast input performance is required.
Required Packages
Before using BufferedReader, the following packages must be imported.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
Syntax of BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Explanation
BufferedReader→ Reads text efficientlyInputStreamReader→ Converts byte stream into character streamSystem.in→ Accepts keyboard input
Java Program to Accept String Input
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class BufferedReaderExample {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your name: ");
String name = br.readLine();
System.out.println("Your name is: " + name);
}
}
Output
Enter your name: Rahul
Your name is: Rahul
Java Program to Accept Integer Input
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class IntegerInputExample {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a number: ");
int number = Integer.parseInt(br.readLine());
System.out.println("Entered number is: " + number);
}
}
Output
Enter a number: 50
Entered number is: 50
How BufferedReader Works
The BufferedReader class reads text input from the keyboard using System.in.
The readLine() method reads the complete line entered by the user as a string.
For numeric input, conversion methods like Integer.parseInt() are used to convert strings into integer values.
Advantages of BufferedReader
- Faster input performance
- Efficient for reading large text input
- Reads complete lines easily
- Useful in competitive programming and large applications
Disadvantages of BufferedReader
- More complex than Scanner class
- Requires exception handling
- Input conversion needed for numeric values
- Less beginner-friendly compared to Scanner
Difference Between BufferedReader and Scanner
| Feature | BufferedReader | Scanner |
|---|---|---|
| Speed | Faster | Slower |
| Package | java.io | java.util |
| Exception Handling | Required | Usually Not Required |
| Numeric Parsing | Manual | Automatic |
| Beginner Friendly | Less | More |
Important Note About BufferedReader
The BufferedReader class is useful for fast input operations in Java. However, beginners often prefer the Scanner class because it is easier to understand and requires less manual conversion.
In competitive programming and performance-based applications, BufferedReader is commonly preferred because of its speed.
Common Methods of BufferedReader
| Method | Description |
|---|---|
readLine() | Reads a complete line |
close() | Closes the stream |
ready() | Checks whether stream is ready |
Frequently Asked Questions
What is BufferedReader in Java?
BufferedReader is a Java class used to read text input efficiently from the keyboard or files.
Which package contains BufferedReader?
The BufferedReader class belongs to the java.io package.
Why is BufferedReader faster?
It reads multiple characters at once using buffering, which improves input performance.
What is readLine() in Java?
The readLine() method reads an entire line of text entered by the user.
Which is better: Scanner or BufferedReader?
Scanner is easier for beginners, while BufferedReader is generally faster and preferred for high-performance input handling.
Conclusion
The BufferedReader class in Java is an efficient way to accept input from the user. It is widely used in Java applications where faster input handling is required.
By learning BufferedReader, students can understand Java input streams, exception handling, and efficient text processing techniques.
