Introduction to I/O Tutorial

As I am sure you are aware, the term I/O is simply short for to Input/Output. The I/O system in Java is actually quite complex, and that is due in most part from the way Java has evolved over time. The first thing to understand in the concept of I/O is a stream, so what is a stream?
In its most basic form a stream simply represents a sequence of data (bytes or unicode characters) in some sort of a sequential queue. A good analogy of an input stream would be a bunch of people waiting to board a plane at an airport gate. Conversely, an output stream would be the people on the airplane that just landed. At that moment in time, both groups are simply queued up and waiting for something to happen. In the world of programming an Input stream contains data that is waiting to be consumed by something, whereas and Output stream contains data that is waiting to be produced into something. Now that we understand what a I/O stream is, we now need to know that there are two basic categories for the type of data contained in those streams: Character and Byte.
Character streams contains data that is represented in Unicode format which makes a character stream a good candidate for things like keyboard input and console output. Byte streams contain binary data this useful for things such as reading and writing to files - just imagine opening up an image file in a text editor, that is a good representation of byte data. In order to support both Byte Streams and Character Streams, Java has two distinct hierarchies that contain many interfaces and classes that essentially do the same thing ... only one for Byte Streams and another for Character Streams.
Throughout all of my tutorial up to this point we have been using the System.out.println() method to display data to the console. System.out is a basic standard output stream which, by default, is the console. The System.in is the standard input stream which, by default, is the keyboard. Over the next few tutorials I will demonstrate several different ways to consume input data from the keyboard. Let's begin by using the System.in.read() method which is the most basic, but not friendly, of the input methods.



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 IOIntro
C:\Java>cd IOIntro
C:\Java\IOIntro>Notepad IOIntro.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 IOIntro {
    public static void main(String args[]) throws IOException {
        int letter = 0;

        System.out.print("Type a letter and press Enter: ");
        letter = System.in.read();
        System.out.println("You typed: " + letter);
        System.out.println("You typed: " + (char) letter);
        System.out.println("--------");

        //while((letter = System.in.read ()) != '\n') {
        //  System.out.println((char) letter);
        //}

        /*
        System.out.print("Type another letter and press Enter: ");
        letter = System.in.read();
        System.out.println("You typed: " + letter);
        System.out.println("You typed: " + (char) letter);
        System.out.println("--------");

        while((letter = System.in.read ()) != '\n') {
            System.out.println((char) letter);
        }

        byte bArray[] = new byte[10];
        System.out.print("Type some letters and press Enter: ");
        System.in.read(bArray);
        System.out.print("You typed: ");
        for(byte b: bArray) {
            System.out.print((char) b);
        }
        */
    }
}

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


C:\Java\IOIntro>javac IOIntro.java
C:\Java\IOIntro>java IOIntro
Type a letter and press Enter:
results vary


Final thoughts

It is important to note that the read() method is actually reading byte data in a primitive int data type, I have just simply coverted it to readable Unicode characters by casting it to type (char).


Tutorials