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.io
package.The example program featured in The "Hello World" Application 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 theSystem
class that implements the standard output stream.System.out
is an instance of thePrintStream
class defined in thejava.io
package. ThePrintStream
class is anOutputStream
that is easy to use. Simply call one of theprintln
, orwrite
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 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 theSystem
class that implements the standard input stream.System.in
is anInputStream
object.InputStream
is an abstract class defined in thejava.io
package that defines the behavior of all sequential input streams in Java. All the input streams defined in thejava.io
package are subclasses ofInputStream
.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 injava.io
(including the streams mentioned on this page:PrintStream
,OutputStream
andInputStream
) and shows you how to use them.
Input and Output Streams |