InputStreamReader Tutorial

The InputStreamReader class can serve multiple purposes, but primarily it is used to convert byte streams to character streams. This tutorial builds on concepts from my Introduction to I/O Tutorial, I highly recommend watching it first. InputStreamReader has several overloaded constructors with each taking an InputStream (like System.in, or FileInputStream) as the first parameter. This tutorial will utilize the constructor that only takes a single parameter. The InputStreamReader class has a method called close() that will close the stream and releases any system resources associated with it. The close() method should always be called once you are done with the input stream.

In Java 7 a new feature called try-with-resources was introduced. Specifically a resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. How do we know if a class is a resource? Simple, if it implements java.lang.AutoCloseable, the class can be considered a resource. I'll demonstrate how this feature works.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md ISR
C:\Java>cd ISR
C:\Java\ISR>Notepad ISR.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


import java.io.*;
class ISR {
    public static void main(String args[]) throws IOException {
        System.out.print("Enter some letters: ");
        InputStreamReader isr = new InputStreamReader(System.in);
        int letters = isr.read();
        System.out.println(isr.ready());
        System.out.print("You entered: ");
        while(isr.ready()) {
            System.out.print((char) letters);
            letters = isr.read();
        }
        isr.close(); // // Closes the stream and releases any system resources associated with it
        System.out.println("\n---------");

        /*try (InputStreamReader isr = new InputStreamReader(System.in)) {
            System.out.print("Enter some letters: ");
            int letters = isr.read();
            System.out.print("You entered: ");
            while(isr.ready()) {
                System.out.print((char) letters);
                letters = isr.read();
            }
            // isr.close(); // try-with-resources automatically calls this so we don't have to worry about missing this step
            System.out.println("\n---------");    
        } catch(IOException e) {
            System.out.println(e.getMessage());
        }*/
    }
}

Now switch back to the command prompt (CMD) and type in javac ISR.java and press Enter.
Now type in java ISR and press Enter.


C:\Java\ISR>javac ISR.java
C:\Java\ISR>java ISR
Enter some letters:
results vary


Final thoughts

InputStreamReader is just a little more useful than using the raw System.in stream, stay tuned to my next tutorial where I will wrap InputStreamReader into a BufferedReader and we will gain more functionality. Try-with-resources is a very valuable feature that you should keep in the back of your mind. You will come across many classes in Java that require a close() method to free up system resources.


Tutorials