BufferedReader Class in Java

BufferedReader Class

This class is used to read text from a character input stream. It provides the efficient reading of the character, arrays and line. It inherits the properties of Reader class.
BufferedReader class used a readLine() method to reads a line of text.

For example:
BufferedReader br = new BufferedReader(System.in);

BufferedReader Class Constructors

BufferedReader (Reader in)
This constructor creates a buffering input stream that uses a default size input buffered.

BufferedReader (Reader in, int size)
This constructor creates a buffering input stream that uses a specified size input buffered.

Example: Getting user input using BufferedReader class

import java.io.*;
public class BufferedDemo
{
    public static void main(String args[])
    {
        try
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter first number: ");
            String n1 = br.readLine();
            System.out.print("Enter second number: ");
            String n2 = br.readLine();
      
            //convert string into integer
            int a = Integer.parseInt(n1);
            int b = Integer.parseInt(n2);
            int sum = a + b;
            System.out.println("Result: "+sum);
          }
          catch(NumberFormatException  nfe)
          {
              System.out.println("Enter only integer value");
           }
           catch(IOException io)
          {
             io.printStackTrace();
          }
      }
}

      
Output: 1
Enter first number: 30
Enter second number: 20
Result: 50
Output: 2
Enter first number: a
Enter second number: 23
Enter only integer value