Java provides a rich set of classes for I/O. A high-level class diagram for the I/O class library is shown here:
All classes in Java inherit from the Object class, and so do the various I/O classes.
The InputStream and OutputStream classes operate on byte data.
The Reader and Writer classes work on characters.
· The java.io.File
class provides the interface to physical files.
Java SE 7 introduced the java.nio.file.Path class, which is considered the equivalent of java.io.File in the new API and provides much more sophisticated functionality.
The byte-oriented files work on 8-bit code and the character-oriented files work on 16-bit Unicode. We’ll begin with the byte-oriented files. Both InputStream and OutputStream are abstract classes from which the various byte-stream-oriented classes derive their functionality.
The InputStream class is a base class for all the input-related classes, and the OutputStream class is a base class for all output-related classes. These two classes provide several methods, such as:
· writeFloat, and more, for reading and writing.
The subclasses provide the implementations of these methods. Examples of these subclasses are:
The Byte Streams
Let’s discuss the various InputStream derived classes first. Adjoining figure shows a few subclasses of InputStream.
As mentioned earlier, the number of classes in the Java I/O package is too large to cover all of them in a post like this. Therefore, we will take a more practical approach and discuss these
classes through programming examples. The first programming example teaches us:
· how to open a file
· read its contents byte by byte
· And then close the file.
The program in the next post determines the length of the specified file in terms of the number of bytes it contains. Note that we are currently focusing on byte-oriented files. When we deal with a text file, we will use character-oriented stream classes, where each character (Unicode) consists of two bytes of data.