Chapter 10. Input and Output: Reading, Writing, and Directory Tricks

10.0 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 other memory devices. 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 12 and 13.

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 Apache Hadoop HDFS, 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 support for reading and writing is in two major parts:

  • The InputStream/OutputStream/Reader/Writer classes, which are the traditional ways of reading/writing files, have been largely ...

Get Java Cookbook, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.