Java Reference
In-Depth Information
Display 10.21
Some Methods of the Class
RandomAccessFile
(part 1 of 3)
The class
RandomAccessFile
is in the
java.io
package.
public
RandomAccessFile(String fileName, String mode)
public
RandomAccessFile(File fileObject, String mode)
Opens the file, does not delete data already in the file, but does position the file pointer at the first
(zeroth) location.
The
mode
must be one of the following:
"r"
Open for reading only.
"rw"
Open for reading and writing.
"rws"
Same as
"rw"
, and also requires that every update to the file's content or metadata be
written synchronously to the underlying storage device.
"rwd"
Same as
"rw"
, and also requires that every update to the file's content be written synchro-
nously to the underlying storage device.
"rws"
and
"rwd"
are not covered in this topic.
public long
getFilePointer()
throws
IOException
Returns the current location of the file pointer. Locations are numbered starting with
0
.
public void
seek(
long
location)
throws
IOException
Moves the file pointer to the specified
location
.
public long
length()
throws
IOException
Returns the length 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 pointer location as returned by the
getFilePointer
method is greater than
newLength
, then after this method returns, the file pointer
location will be equal to
newLength
.
If the present length of the file as returned by the
length
method is smaller than
newLength
, then the
file will be extended. In this case, the contents of the extended portion of the file are not defined.
public void
close()
throws
IOException
Closes the stream's connection to a file.
public void
write(int b)
throws
IOException
Writes the specified byte to the file.
public void
write(
byte
[] a)
throws
IOException
Writes
a.length
bytes from the specified byte array to the file.