- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
BufferedReader class in Java.
The BufferedReader class of Java is used to read the stream of characters from the specified source (character-input stream). The constructor of this class accepts an InputStream object as a parameter.
This class provides a method named read() and readLine() which reads and returns the character and next line from the source (respectively) and returns them.
Instantiate an InputStreamReader class bypassing your InputStream object as a parameter.
Then, create a BufferedReader, bypassing the above obtained InputStreamReader object as a parameter.
Now, read data from the current reader as String using the readLine() or read() method.
Example
The following Java program demonstrates how to read integer data from the user using the BufferedReader class.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class Employee{ String name; int id; int age; Employee(String name, int age, int id){ this.name = name; this.age = age; this.id = id; } public void displayDetails(){ System.out.println("Name: "+this.name); System.out.println("Age: "+this.age); System.out.println("Id: "+this.id); } } public class ReadData { public static void main(String args[]) throws IOException { BufferedReader reader =new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter your name: "); String name = reader.readLine(); System.out.println("Enter your age: "); int age = Integer.parseInt(reader.readLine()); System.out.println("Enter your Id: "); int id = Integer.parseInt(reader.readLine()); Employee std = new Employee(name, age, id); std.displayDetails(); } }
Output
Enter your name: Krishna Enter your age: 25 Enter your Id: 1233 Name: Krishna Age: 25 Id: 1233
- Related Articles
- How to read integers from a file using BufferedReader in Java?
- What is the class "class" in Java?
- Final class in Java
- Abstract class in Java
- Object class in Java
- StringTokenizer class in Java
- Timer Class in Java
- Static class in Java
- GregorianCalendar Class in Java
- Date Class in Java
- CopyOnWriteArraySet Class in Java
- CopyOnWriteArrayList Class in Java
- JavaBean class in Java
- Inner class in Java
- ArrayBlockingQueue Class in Java

Advertisements