Curriculum
Course: Learn Java Programming
Login

Curriculum

Learn Java Programming

Text lesson

Taking Input Using BufferedReader and InputStream Reader in Java

[post-views]

 

In this lesson, you will learn.

  • I/O Streams in Java
  • Byte Stream
  • Character Stream
  • InputStreamReader and BufferedReader Class

 

I/O Stream in Java

  • Java programs perform I/O (Input/Output) through streams.
  • I/O streams are used to read data from a source or to write data to a destination.
  • A stream is an abstraction that either produces or consumes information.
  • A stream is linked to a physical device by the Java I/O system.

 

Types of I/O Stream in Java

There are two types of streams in Java as shown below.

 

Note: Input/Output Stream Classes are defined in the java.io package.

 

Byte Stream

  • Byte streams are primarily used for handling input and output of bytes.

Byte Stream has two abstract classes.

  1. Input Stream and
  2. OutputStream class.

InputStream

Used to read data from a source. It has some key classes summarized below.

Key Classes of InputStream:

  • FileInputStream: Reads data from a file.
  • BufferedInputStream: Adds buffering to an input stream.
  • DataInputStream: Allows reading of Java primitive data types.
  • ObjectInputStream: Used for reading objects.

OutputStream

Used to write data to a destination. It has some key classes summarized below.

Key Classes of OutputStream:

  • FileOutputStream: Writes data to a File.
  • BufferedOutputStream: Adds buffering to an output stream.
  • DataOutputStream: Allows writing of Java primitive data types.
  • ObjectOutputStream: Used for writing objects.

 


Character Stream

  • Character streams are used for handling input and output of characters, taking into account character encoding.

Character Streams are represented by two abstract classes.

  1. Reader
  2. Writer

Reader

Used to read data from a source. It has some key classes summarized below.

Key Classes:

  • FileReader: Reads character files.
  • BufferedReader: Buffer’s characters for efficient reading.
  • InputStreamReader: Bridges byte streams to character streams.
  • StringReader: Reads characters from a string.

Writer

Used to write data to a destination. It has some key classes summarized below.

Key Classes:

  • FileWriter: Writes characters to a file.
  • BufferedWriter: Buffer’s characters for efficient writing.
  • OutputStreamWriter: Bridges character streams to byte streams.
  • StringWriter: Writes characters to a string.

 

2. Understanding InputStreamReader and BufferedReader Stream Classes

 

InputStreamReader Class

  • The InputStreamReader is a bridge from byte streams to character streams.
  • It reads bytes and decodes them into characters using a specified charset.

Syntax

InputStreamReader isr = new InputStreamReader(System.in);

This is usually used to read bytes from an input stream (like System.in) and convert them into characters.

System.in is a standard input stream, typically connected to keyboard input.

 

BufferedReader Class

BufferedReader reads text from a character input stream, buffering characters and providing efficient reading of characters, arrays, and lines.

Syntax

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

It buffers the input for efficient reading of characters, arrays, and lines and stores in an object br.

 

Example 1: Reading a Single String

package ch3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class SingleStringInput {
    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("Hello, " + name + "!");
    }
}

 

Output

Enter your name:
Amit Kumar
Hello, Amit Kumar!

 

Example 2: Reading Integer Input and Performing Arithmetic

package ch3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class IntegerArithmetic {
    public static void main(String[] args) throws IOException
    {
    	BufferedReader reader = new BufferedReader(
    			new InputStreamReader(System.in));
    	
    	System.out.println("Enter first number:");
        int firstNumber = Integer.parseInt(reader.readLine());
        
        System.out.println("Enter second number:");
        int secondNumber = Integer.parseInt(reader.readLine());
        
        int sum = firstNumber + secondNumber;
        System.out.println("The sum is: " + sum);
    }
}

 

Output

Enter first number:
10
Enter second number:
15
The sum is: 25

Explanation

Integer.parseInt(String s): This static method from the Integer class attempts to parse a String as an integer value.

It throws NumberFormatException if the string cannot be parsed as an integer.

IOException: This exception is thrown when an I/O operation fails or is interrupted. In this context, it could be thrown by the readLine() method if an I/O error occurs while reading input from the user.

 

Example 3: Reading a Single Character

package ch3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ReadSingleCharacter {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader
        		(new InputStreamReader(System.in));
        System.out.println("Enter a character: ");
        int charAsInt = reader.read(); // Reads a single character
        char character = (char) charAsInt;
        System.out.println("You entered: " + character);
        reader.close();
    }
}

 

Output

First Run

Enter a character: 
A
You entered: A

Second Run

Enter a character: 
Hot Java
You entered: H

 

Bonus!

Example 4: Reading Multiple Lines Until Specific Input (e.g., “stop”)

package ch3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MultiLineInput {
    public static void main(String[] args) throws IOException {
    	
    	BufferedReader reader = new BufferedReader
    			(new InputStreamReader(System.in));
    	
    	String inputLine;
    	System.out.println("Enter lines of text (type 'stop' to quit):");
    	
    	while (!(inputLine = reader.readLine()).equalsIgnoreCase("stop")) 
    	{
    		System.out.println("You entered: " + inputLine);
    	}
    }
}

 

Output

Enter lines of text (type 'stop' to quit):
Java is an object oriented programming language.
You entered: Java is an object oriented programming language.
Java is simple and platform independent language.
You entered: Java is simple and platform independent language.
stop

 


 


 

End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

There are no reviews yet. Be the first one to write one.

 

 

Submit a Review