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.


Purpose and Function of the Java Stream Class used in Java 1.1.

In Java 1.1, the "Stream" class referred to classes designed for input and output operations, specifically byte streams and character streams. Their primary purpose and function were:
  1. Handling Input/Output Operations:
    • Reading from Input Sources: Stream classes facilitated reading data from various sources, such as files, network connections, or the keyboard.
    • Writing to Output Destinations: They also enabled writing data to different destinations like files, network connections, or the console.
  2. Processing Data in Different Formats:
    • Byte Streams: Classes like `InputStream` and `OutputStream` dealt with data in the form of raw bytes. This was suitable for handling binary data like images or audio.
    • Character Streams: Java 1.1 introduced `Reader` and `Writer` classes for processing data as sequences of Unicode characters. This improved support for internationalization and handling text data.


Key Points to Note:
  • Java 1.1 Context: The concept of Java Streams has evolved significantly since Java 1.1. In modern Java, the term "Stream" primarily refers to the `java.util.stream.Stream` API introduced in Java 8, which is entirely different from the I/O streams in Java 1.1.
  • Focus on I/O: In Java 1.1, Stream classes were fundamentally about handling input and output operations, facilitating the flow of data between the program and external sources/destinations.
  • Byte vs. Character Orientation: The distinction between byte streams and character streams was crucial for handling different data types appropriately.

In Summary: The "Stream" class in Java 1.1 played a vital role in managing input and output operations, allowing programs to interact with the external world by reading and writing data in either byte or character formats. Understanding these foundational I/O stream classes is helpful even in modern Java, as they continue to be used for various file and network operations.

SEMrush Software