Read data using BufferedReader class
The another way to read input from keyboard is using java.io.BufferedReader class. It reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. The buffer size may be specified, or the default size may be used. InputStreamReader class can be used to read data from keyboard.
It performs two tasks:
- Connects to input stream of keyboard and
- Converts the byte-oriented stream into character-oriented stream
readLine( ) is a method of BufferedReader class which is used to read a line of text. IOException is a type of exception which is thrown by readLine( ) method. Some of the important methods of BufferedReader are given below:
- void close( ) : Closes the stream and releases any system resources associated with it.
- int read( ) : Reads a single character.
- int read(char[ ] cbuf, int off, int len) : Reads characters into a portion of an array.
- String readLine( ) : Reads a line of text.
PROGRAM
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class ReadDataUsingBufferedReader {
public static void main(String[] args) throws IOException {
InputStreamReader ios = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ios);
System.out.println("Enter ur name...");
String name = br.readLine();
System.out.println("Hello, "+name);
}
}
OUTPUT
C:\>javac ReadDataUsingBufferedReader.java C:\>java ReadDataUsingBufferedReader Enter ur name... Rahul Hello, Rahul