Java Reference
In-Depth Information
Chapter 10. Input and Output
Introduction
Most programs need to interact with the outside world, and one common way of doing so is
by reading and writing files. Files are normally on some persistent medium such as a disk
drive, and, for the most part, we shall happily ignore the differences between files on a hard
disk (and all the operating system-dependent filesystem types), a USB drive or SD card, a
DVD-ROM, and others. For now, they're just files. And, like most other languages and
OSes, Java extends the “reading and writing” model to network (socket) communications,
which we'll touch on in Chapters 13 and 16 .
Java provides many classes for input and output; they are summarized in Figure 10-1 . This
chapter covers all the normal input/output operations such as opening/closing and reading/
writing files. Files are assumed to reside on some kind of file store or permanent storage.
Distributed filesystems such as Sun's Network File System (NFS, common on Unix and
available for Windows), SMB (the Windows network filesystem, available for Unix via the
open source Samba program), and FUSE (Filesystem in User SpacE, implementations for
most Unix/Linux systems) are assumed to work “just like” disk filesystems, except where
noted.
The Formatter and Scanner classes provide formatting and scanning, of course. Formatter
allows many formatting tasks to be performed either into a String or to almost any output
destination. Scanner parses many kinds of objects, again either from a String or from al-
most any input source. These are new and very powerful; each is given its own recipe in this
chapter.
Finally, this chapter provides only minimal coverage of the moribund Java Communications
API, designed to read and write on serial and parallel ports. Previous editions of this topic
contained full coverage of this API, but it is little used now, and even less maintained.
Streams and Readers/Writers
Java provides two sets of classes for reading and writing. The Stream section of package
java.io (see Figure 10-1 ) is for reading or writing bytes of data. Older languages tended to
Search WWH ::




Custom Search