|
|
Input and Output Streams |
If you have been using the standard input and output streams, then you have, perhaps unknowingly, been using input and output streams from thejava.iopackage.The example program featured in The "Hello World" Application
uses
System.out.printlnto display the text "Hello World!" to the user.class HelloWorldApp { public static void main (String[] args) { System.out.println("Hello World!"); } }System.outrefers to an output stream managed by theSystemclass that implements the standard output stream.
System.outis an instance of thePrintStreamclass defined in the
java.iopackage. ThePrintStreamclass is anOutputStreamthat is easy to use. Simply call one of theprintln, orwritemethods to write various types of data to the stream.
PrintStreamis 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
is structured uses
System.in.readto 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.inrefers to an input stream managed by theSystemclass that implements the standard input stream.System.inis anInputStreamobject.
InputStreamis an abstract class defined in thejava.iopackage that defines the behavior of all sequential input streams in Java. All the input streams defined in thejava.iopackage are subclasses ofInputStream.InputStreamdefines 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.iopackage. The remainder of this lesson gives an overview of the streams injava.io(including the streams mentioned on this page:PrintStream,OutputStreamandInputStream) and shows you how to use them.
|
|
Input and Output Streams |