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:
  1. void close( ) : Closes the stream and releases any system resources associated with it.
  2. int read( ) : Reads a single character.
  3. int read(char[ ] cbuf, int off, int len) : Reads characters into a portion of an array.
  4. 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

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to input age from user and throw user-defined exception if entered age is negative