Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

BufferedReader Class in Java

There are two ways to take input from the user in Java
 Using Scanner Class(java.util package)
 Using BufferedReader Class(java.io package)
Scanner and BufferedReader both classes are used to read
input from external System.
Scanner is normally used when we know input is of
type String or of primitive types and BufferedReader is
used to read text from character Stream.
BufferedReader has significantly large buffer(8 KB)
than Scanner(1 KB) which means if you are reading long
string from file, you should use BufferedReader but for
short, input and input other than String, you can use
Scanner class.
Syntax of BufferedReader Class
BufferedReader br=new BufferedReader(new InputStreamReader(System.in))
Class nm object Inbuilt class take data from keyboard

There is a function”readLine()” Which is used to read one


line of input. br.readLine();
readLine() methods takes data in the form of “String”
only .Suppose you want to take data in the form of
primitive data type that time we have to do parsing.
“Parsing is to read the value of one object to convert it to
another data type.”
e.g I want to take integer value from user using
BufferedReader class that time following code we have to
write.
1. Take one integer variable for assigning data taken from
readLine();
int a=Interger.parseInt(br.readLine());
float=Float.parseFloat(br.readLine());
double=Double.parseDouble(br.readLine());
like that,
But ,if you want to take String data from user using
BufferedReader Class then no need to do parsing because
readLine() method takes data in the form of String only.

You might also like