|
| |
Instances of class RandomAccessFile support both
reading and writing to a random access file. A random access file behaves
like a large array 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 output operations are also available; output
operations write bytes starting at the file pointer and advance the file
pointer past the bytes written. Output operations that write past the
current end of the implied array cause the array to be extended. The file
pointer can be read by the getFilePointer method and set by the
seek method.
It is generally true of all the reading routines in this class that if
end-of-file is reached before the desired number of bytes has been read, an EOFException
is thrown. If any byte cannot be read for any reason other than end-of-file,
an IOException other than EOFException
is thrown. In particular, an IOException may be thrown
if the stream has been closed.
RandomAccessFile has two constructors:
| Constructor |
Description |
public RandomAccessFile(File file,
String mode)
throws IOException
|
Creates a random access file stream to read from, and
optionally to write to, the file specified by the File
argument. |
public RandomAccessFile(String name,
String mode)
throws FileNotFoundException
|
Creates a random access file stream to read from, and
optionally to write to, a file with the specified name. |
It implements the DataInput and DataOutput interfaces, and so it supports all
the methods required by them.
In addition, it implements the following methods:
| Method |
Description |
public void close()
throws IOException
|
Closes this random access file stream and releases
any system resources associated with the stream. A closed random
access file cannot perform input or output operations and cannot be
reopened. |
public final FileDescriptor getFD()
throws IOException
|
Returns the opaque file descriptor object associated
with this stream. |
public long getFilePointer()
throws IOException
|
Returns the current offset in this file. |
public long length()
throws IOException
|
Returns the length of this file. |
public void seek(long pos)
throws IOException
|
Sets the file-pointer offset, measured from the
beginning of this file, at which the next read or write occurs. The
offset may be set beyond the end of the file. Setting the offset
beyond the end of the file does not change the file length. The file
length will change only by writing after the offset has been set
beyond the end of the file. |
public void setLength(long newLength)
throws IOException
|
Sets the length of this file.
If the present length of the file as returned by the length
method is greater than the newLength argument then the
file will be truncated. In this case, if the file offset as returned
by the getFilePointer method is greater then newLength
then after this method returns the offset will be equal to newLength.
If the present length of the file as returned by the length
method is smaller than the newLength argument then the
file will be extended. In this case, the contents of the extended
portion of the file are not defined.
|
|