Main Page

Previous Next

Streams and the New I/O Capability

The package that supports stream input/output is java.io, and it is vast. It defines around fifty classes, many of which have a large number of methods. It is therefore quite impractical to go into them all in detail in a book of this kind. Refer to the java documentation for more information. Our strategy in this, and the following three chapters, will be to take a practical approach. The idea is to provide an overall grounding of the concepts involved, and to equip you with enough detailed knowledge to be able to do a number of specific, useful, and practical things in your programs. These are:

  • To be able to read data of various kinds from the keyboard.

  • To be able to create formatted output to the command line.

  • To be able to read and write files containing basic data.

  • To be able to read and write files containing objects.

To achieve this, we will give you an overview of what the important stream classes do, and how they interrelate, together with the classes that operate on streams. We will go into detail selectively, just exploring the classes and methods that we need to accomplish specific things. We will also be sticking to the latest and greatest I/O capability in the JDK 1.4, which makes it unnecessary to delve into a lot of the original stream classes.

Up to and including Java 1.3, the only way to read and write files was to use a stream. Java 1.4 introduced a new I/O capability in the java.nio and java.nio.channels packages for reading and writing files that contain data of the primitive Java types, including strings. This capability completely supersedes the stream I/O capability in this context. It is much more efficient, and in many ways easier to use, so we will limit our discussions of streams for handling files to the extent necessary to understand the new I/O capability. We will go into the new I/O capability in detail in the next two chapters.

Two areas where you must still use the facilities provided by the stream classes are reading from the keyboard, and writing to the command line. We will cover both of these in this chapter along with some general aspects of the stream classes and the relationships between them. The new I/O capability does not provide for objects to be written and read, so you still need to use streams for this. We will look into how we read and write objects to a file in Chapter 11 Serializing Objects.

Previous Next
JavaScript Editor Java Tutorials Free JavaScript Editor