THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

data[i] = in.readDouble();
in.close();
return data;
}


The writeData method first opens the file and writes the array length. It then loops, writing the contents of
the array. The file can be read into an array with readData. These methods can be rewritten more simply
using the Object streams you will learn about in Section 20.8 on page 549.


Exercise 20.7: Add a method to the Attr class of Chapter 3 that writes the contents of an object to a
DataOutputStream and add a constructor that will read the state from a DataInputStream.


20.7. Working with Files


The java.io package provides a number of classes that help you work with files in the underlying system.
The File stream classes allow you to read from and write to files and the FileDescriptor class allows
the system to represent underlying file system resources as objects. RandomAccessFile lets you deal with
files as randomly accessed streams of bytes or characters. Actual interaction with the local file system is
through the File class, which provides an abstraction of file pathnames, including path component
separators, and useful methods to manipulate file names.


20.7.1. File Streams and FileDescriptor


The File streamsFileInputStream, FileOutputStream, FileReader, and FileWriterallow
you to treat a file as a stream for input or output. Each type is instantiated with one of three constructors:



  • A constructor that takes a String that is the name of the file.

  • A constructor that takes a File object that refers to the file (see Section 20.7.3 on page 543).

  • A constructor that takes a FileDescriptor object (see below).


If a file does not exist, the input streams will throw a FileNotFoundException. Accessing a file
requires a security check and a SecurityException is thrown if you do not have permission to access
that filesee "Security" on page 677.


With a byte or character output stream, the first two constructor types create the file if it does not exist, or
truncate it if it does exist. You can control truncation by using the overloaded forms of these two constructors
that take a second argument: a boolean that, if true, causes each individual write to append to the file. If
this boolean is false, the file will be truncated and new data added. If the file does not exist, the file will
be created and the boolean will be ignored.


The byte File streams also provide a getChannel method for integration with the java.nio facilities. It
returns a java.nio.channels.FileChannel object for accessing the file.


A FileDescriptor object represents a system-dependent value that describes an open file. You can get a
file descriptor object by invoking getFD on a File byte streamyou cannot obtain the file descriptor from
File character streams. You can test the validity of a FileDescriptor by invoking its boolean valid
methodfile descriptors created directly with the no-arg constructor of FileDescriptor are not valid.


FileDescriptor objects create a new File stream to the same file as another stream without needing to
know the file's pathname. You must be careful to avoid unexpected interactions between two streams doing
different things with the same file. You cannot predict what happens, for example, when two threads write to

Free download pdf