Java Reference
In-Depth Information
CHAPTER 3
Input/Output
A computer program isn't much good unless it can communicate with the outside
world. This communication often takes the form of input/output (I/O); I/O capa-
bilities are a fundamental feature of any programming platform. In Java, I/O is
done with the classes and interfaces of the java.io package. This chapter demon-
strates many of the input/output capabilities of java.io . The examples here show
you how to:
Read and write files
List directories and obtain file size and date information
Use various Java stream classes
Define customized stream subclasses
The techniques introduced in this chapter are also used in other places in this
topic. We'll see many examples that use streams for input and output in Chapter 5,
Networking , and we'll see a specialized kind of I/O in Chapter 9, Object Serializa-
tion .
Files and Streams
One of the commonly used classes in the java.io package is File . This class is
somewhat misleadingly named, as it represents a filename (or directory name),
rather than a file itself. Because files (and directories) have different naming con-
ventions under different operating systems, Java provides the File class to try to
hide some of those differences. The File class also defines various methods for
operating on files as a whole: deleting files, creating directories, listing directories,
querying the size and modification time of a file, and so on.
While the File class provides methods to manipulate directories and the files
within those directories, it doesn't provide any methods that manipulate the con-
tents of the files. In other words, it doesn't provide any way to read or write the
bytes or characters that are contained in files. In Java, sequential file I/O is
Search WWH ::




Custom Search