Previous | Next | Trail Map | Writing Java Programs | Input and Output Streams


Your First Encounter with I/O in Java (1.1notes)

If you have been using the standard input and output streams, then you have, perhaps unknowingly, been using input and output streams from the java.io package.

The example program featured in The "Hello World" Application(in the Getting Started trail) uses System.out.println to display the text "Hello World!" to the user.

class HelloWorldApp {
    public static void main (String[] args) {
        System.out.println("Hello World!");
    }
}
System.out refers to an output stream managed by the System(in the API reference documentation) class that implements the standard output stream. System.out is an instance of the PrintStream class defined in the java.io package. The PrintStream class is an OutputStream that is easy to use. Simply call one of the print, println, or write methods to write various types of data to the stream.

PrintStream is one of a set of streams known as filtered streams that are covered in later in this lesson.

Similarly, the example program around which The Nuts and Bolts of the Java Language(in the Writing Java Programs trail) is structured uses System.in.read to read in characters entered at the keyboard by the user.

class Count {
    public static void main(String[] args)
        throws java.io.IOException
    {
        int count = 0;

        while (System.in.read() != -1)
            count++;
        System.out.println("Input has " + count + " chars.");
    }
}
System.in refers to an input stream managed by the System class that implements the standard input stream. System.in is an InputStream object. InputStream is an abstract class defined in the java.io package that defines the behavior of all sequential input streams in Java. All the input streams defined in the java.io package are subclasses of InputStream. InputStream defines a programming interface for input streams that includes methods for reading from the stream, marking a location within the stream, skipping to a mark, and closing the stream.

So you see, you are already familiar with some of the input and output streams in the java.io package. The remainder of this lesson gives an overview of the streams in java.io (including the streams mentioned on this page: PrintStream, OutputStream and InputStream) and shows you how to use them.


Previous | Next | Trail Map | Writing Java Programs | Input and Output Streams