Java Reference
In-Depth Information
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
ob-
ject for accessing the file.
A
FileDescriptor
object represents a system-dependent value that de-
scribes 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 an-
other stream without needing to know the file's pathname. You must be
careful to avoid unexpected interactions between two streams doing dif-
ferent things with the same file. You cannot predict what happens, for
example, when two threads write to the same file using two different
FileOutputStream
objects at the same time.
The
flush
method of
FileOutputStream
and
FileWriter
guarantees that the
buffer is flushed to the underlying file. It does not guarantee that the
data is committed to diskthe underlying file system may do its own buf-
fering. You can guarantee that the data is committed to disk by invok-
ing the
sync
method on the file's
FileDescriptor
object, which will either
force the data to disk or throw a
SyncFailedException
if the underlying
system cannot fulfill this contract.
20.7.2.
RandomAccessFile
The
RandomAccessFile
class provides a more sophisticated file mechanism
than the
File
streams do. A random access file behaves like a large ar-
ray of bytes stored in the file system. There is a kind of cursor, or index
into the implied array, called the
file pointer;
input operations read bytes
starting at the file pointer and advance the file pointer past the bytes
read. If the random access file is created in read/write mode, then out-