26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

898 Files and Streams Chapter 16<br />

ing platform represents files or streams. In some cases, the end-of-file indication occurs as<br />

an exception. In other cases, the indication is a return value from a method invoked on a<br />

stream-processing object. We demonstrate both cases in this chapter.<br />

A <strong>Java</strong> program opens a file by creating an object and associating a stream of bytes<br />

with the object. <strong>Java</strong> also can associates streams of bytes associated with devices. In fact,<br />

<strong>Java</strong> creates three stream objects that are associated with devices when a <strong>Java</strong> program<br />

begins executing—System.in, System.out and System.err. The streams associated<br />

with these objects provide communication channels between a program and a particular<br />

device. For example, object System.in (the standard input stream object) normally<br />

enables a program <strong>to</strong> input bytes from the keyboard, object System.out (the standard<br />

output stream object) normally enables a program <strong>to</strong> output data <strong>to</strong> the screen and object<br />

System.err (the standard error stream object) normally enables a program <strong>to</strong> output<br />

error messages <strong>to</strong> the screen. Each of these streams can be redirected. For System.in,<br />

this enables the program <strong>to</strong> read bytes from a different source. For System.out and<br />

System.err, this enables the output <strong>to</strong> be sent <strong>to</strong> a different location, such as a file on<br />

disk. Class System provides methods setIn, setOut and setErr <strong>to</strong> redirect the standard<br />

input, output and error streams.<br />

<strong>Java</strong> programs perform file processing by using classes from package java.io. This<br />

package includes definitions for the stream classes, such as FileInputStream (for<br />

byte-based input from a file), FileOutputStream (for byte-based output <strong>to</strong> a file),<br />

FileReader (for character-based input from a file) and FileWriter (for characterbased<br />

output <strong>to</strong> a file). Files are opened by creating objects of these stream classes that<br />

inherit from classes InputStream, OutputStream, Reader and Writer, respectively.<br />

Thus, the methods of these stream classes can all be applied <strong>to</strong> file streams as well.<br />

To perform input and output of data types, objects of class ObjectInputStream,<br />

DataInputStream, ObjectOutputStream and DataOutputStream will be<br />

used <strong>to</strong>gether with the byte-based file stream classes FileInputStream and File-<br />

OutputStream. Figure 16.3 summarizes the inheritance relationships of many of the<br />

<strong>Java</strong> I/O classes (abstract classes are shown in italic font). The following discussion<br />

overviews the capabilities of each of the classes in Fig. 16.3.<br />

<strong>Java</strong> offers many classes for performing input/output. This section briefly overviews<br />

many of these classes and explains how they relate <strong>to</strong> one another. In the rest of the chapter,<br />

we use several of these stream classes as we implement a variety of file-processing programs<br />

that create, manipulate and destroy sequential-access files and random-access files.<br />

We also include a detailed example on class File, which is useful for obtaining information<br />

about files and direc<strong>to</strong>ries. In Chapter 17, Networking, we use stream classes extensively<br />

<strong>to</strong> implement networking applications.<br />

0 1 2 3 4 5 6 7 8 9<br />

Fig. Fig. Fig. 16.2 16.2 <strong>Java</strong>’s view of a file of n bytes.<br />

...<br />

...<br />

n-1<br />

end-of-file marker

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!