Java Reference
In-Depth Information
Chapter 8
Understanding Streams
WHAT YOU WILL LEARN IN THIS CHAPTER
• What a stream is and what the main classes that Java provides to support stream operations are
• What stream readers and writers are and what they are used for
• How to read data from the keyboard
• How to format data that you write to the command line
This is the first of five chapters devoted to input and output. This chapter introduces streams , and deals with
keyboard input and output to the command line. In subsequent chapters you learn how to work with files.
STREAMS AND INPUT/OUTPUT OPERATIONS
Streams are fundamental to input and output in your programs in the most instances. The package that sup-
ports stream input/output primarily is java.io but other packages such as java.nio.file define stream
classes, too. The java.io defines a large number of classes and interfaces, many of which have a significant
number of methods. It is quite impractical to go into them all in detail in this topic so my strategy in this and
in the following chapters discussing file I/O is to take a practical approach. I provide an overall grounding
in the concepts and equip you with enough detailed knowledge of the classes involved to enable you to do a
number of specific, useful, and practical things in your programs. These are:
• To be able to read data from the keyboard
• To be able to write formatted output to a stream, such as System.out
• To be able to read and write files containing strings and basic types of data
• To be able to read and write files containing objects
To achieve this, I first give you an overview of the important stream classes in this chapter and how they
interrelate. I'll go into the detail selectively, just exploring the classes and methods that you need to accom-
plish the specific things I noted.
Two areas where you must use the facilities provided by the stream classes are reading from the keyboard
and writing to the command line or the console window of an IDE. I cover both of these in this chapter along
with some general aspects of the stream classes and the relationships between them. You learn about how you
use streams to read and write binary and character data files in Chapters 10 and 11. You look into how you
read from and write objects to a file in Chapter 12.
UNDERSTANDING STREAMS
Search WWH ::




Custom Search