InputStreamReader Java

InputStreamReader and OutputStreamWriter are two IO Streams linking byte streams with character streams. They are known as wrapper streams.

1. What is a Wrapper Stream?

It is a good question not very clear to a Learner. I put it elaborately. Like we have wrapper classes which wraps primitive data types to give object form, we have wrapper streams to wrap byte streams so that they can be linked with character streams.

2. Why to link byte stream to a character stream? to

Sometimes it is necessary to link byte stream to character stream. See the following two statements.

   1.  DataInputStream dis = new DataInputStream(System.in);  

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

We know stream is a carrier of data from one place to another in the system. System.in is a Java stream that is linked to operating system’s in stream (other two streams are out and err). OS in stream carries data from Keyboard to CPU. That is, System.in intercepts the OS in stream and reads from it. in stream contains Keyboard input. Finally to say, System.in reads from Keyboard input.

In the 1st statement, both System.in and DataInputStream are byte streams. Being both byte streams, they are compatible for linking and infact System.in is passed to DataInputStream constructor.

In the 2nd statement, BufferedReader is a character stream and as such is not compatible to link with System.in. Here comes the need of a wrapper stream. Wrapper stream is a converter or an adapter or a link between byte stream and character stream. There are two wrapper streams in IO package – InputStreamReader and OutputStreamWriter. Here, InputStreamReader is used to link System.in with BufferedReader.

System.in is passed to the constructor of InputStreamReader. Inturn, InputStreamReader object is passed to the constructor of BufferedReader. This is how InputStreamReader comes in between System.in and BufferedReader.

Following example gives the usage of InputStreamReader to take Keyboard input.

import java.io.*;
public class ConsoleInput
{
 public static void main(String args[]) throws IOException
 {
   InputStreamReader isr = new InputStreamReader(System.in);
   BufferedReader br = new BufferedReader(isr);

   System.out.println("Enter Your Employer Name:");
   String name = br.readLine();

   System.out.println("Enter Your Basic Salary:");
   String salary1 = br.readLine();
   int salary2 = Integer.parseInt(salary1);

   System.out.println("Enter Your House Rent:");
   String rent1 = br.readLine();
   double rent2 = Double.parseDouble(rent1);

   System.out.println("Your name is " + name);
   System.out.println("Your Total Salary is " + (salary2+rent2));
 }
}

InputStreamReader JavaOutput Screenshot on InputStreamReader Java

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

The above two statements can be simplified as one using anonymous object of InputStreamReader.

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

Once linked, readLine() method reads a line at a time from Keyboard and returns it as a string. Even a integer value or double value entered from Keyboard is read and returned as string. The string is parsed to int and double using parsing methods parseInt() and parseDouble.

Like wrapper stream group, many groups exist in Java. A few are given for your reference.

1. Wrapper classes
2. Listeners
3. Adapters
4. Layout managers
5. Byte streams
6. Character streams
7. Print streams
8. Filter streams
9. Thread groups
10. Components
11. Containers

Leave a Comment

Your email address will not be published.