J.E.D.I.
12 Advanced IO Streams
12.1 Objectives
In a previous module, youve learned how to get user input and manipulate files using streams. You will now learn more about streams and other stream classes.
After completing this lesson, you should be able to: 1. Enumerate the general stream types
2. Use the File class and its methods • Character and Byte Streams
• Input and Output Streams • Node and Filter Streams
3. Use the different InputOutput classes • Reader
• Writer • InputStream
• OutputStream
4. Explain the concept of stream chaining 5. Define serialization
6. Explain the use of the transient keyword 7. Write and read from an object stream
12.2 General Stream Types
12.2.1 Character and Byte Streams
As mentioned before, there are generally two types of streams, the character and byte streams. Let us just review the basic difference between the two. Byte streams are file or
device abstractions for binary data while character streams are for Unicode characters.
The InputStream class is the abstract root class of all input byte streams whereas the OutputStream class is the abstract root class of all output byte streams. For character
streams, the corresponding superclass of all classes are the Reader and the Writer class, respectively. Both classes are abstract classes for reading and writing to character
streams.
12.2.2 Input and Output Streams
Streams are also categorized on whether they are used for reading or writing to streams. Although it is already quite obvious, allow me to define these types of streams. You are
allowed to read from input streams but not write to them. On the other hand, you are allowed to write to output streams but not read from them.
The InputStream class and the Reader class are the superclasses of all input streams. The OutputStream class and the Writer class are the root classes of all output streams.
Input streams are also known as source streams since we get information from these
Introduction to Programming II Page 158
J.E.D.I.
streams. Meanwhile, output streams are also called sink streams.
12.2.3 Node and Filter Streams
Now, the java.io package distinguishes between node and filter streams. A node stream is a stream with the basic functionality of reading or writing from a specific location such
as a disk or from the network. Types of node streams include files, memory and pipes. Filter streams, on the other hand, are layered onto node streams between threads or
processes to provide additional functionalities not found in the node stream by themselves. Adding layers to a node stream is called stream chaining.
The succeeding sections gives an overview of the different stream classes. For a complete list of these classes, please refer to Javas API documentation.
Introduction to Programming II Page 159
J.E.D.I.
12.3 The File Class