Java Input/Output  «Prev   Next»

Lesson 2The I/O package
ObjectiveUsing the Java Stream Class

Using Java Stream Class

Java I/O (input/output) is defined in terms of streams and streams are ordered sequences of data that have a source (in the case of input streams) or a destination (in the case of output streams).
Like Java's other classes, the java.io classes are platform-independent. Therefore, using Java's I/O classes and methods isolate programmers from the specific details of the underlying operating system.
Java's Stream classes can be associated with a file or device. For example, using Java's Stream classes, you can read from and write to files, and this code will work on any platform.

This diagram shows how your code interacts with Input and Output Streams
This diagram shows how your code interacts with Input and Output Streams.

Java File Class

There is also a class called File. Instances of this class are used to locate and identify files; Java's Stream classes are used to access the data in the files. In addition to the File class, java.io defines two basic types of classes:
  1. InputStream and
  2. OutputStream.
Both InputStream and OutputStream are abstract classes. Their subclasses implement the specifics that you will work with when reading from a stream, such as a file or network connection. Here is the class hierarchy for the stream classes you will work with. (As you can see, the File class stands outside of the stream classes.)

This diagram shows the relationship between the Object and Input and Output Stream classes
This diagram shows the relationship between the Object and Input and Output Stream classes.

Input and output Streams

Class InputStream defines methods for reading bytes into a buffer from a resource (such as a file), skipping bytes, and checking to see how many bytes are currently available to be read. The method that's abstract is read(), and each of its subclasses implements the specifics of how to go about reading. Class OutputStream defines methods for writing bytes from a buffer to a resource. The method that is abstract is write(); each subclass implements the specifics of how to go about writing.