This class allows you to read and write
arbitrary bytes, text, and primitive Java data types from or to any
specified location in a file. Because this class provides random,
rather than sequential, access to files, it is neither a subclass of
InputStream nor of
OutputStream, but provides an entirely independent
method for reading and writing data from or to files.
RandomAccessFile implements the same interfaces as
DataInputStream and
DataOutputStream, and thus defines the same
methods for reading and writing data as those classes do.
The seek(
) method provides random access to the file; it is used to
select the position in the file where data should be read or written.
The various read and write methods update this file position so that
a sequence of read or write operations can be performed on a
contiguous portion of the file without having to call the
seek( ) method before each read or write.
The mode argument to the constructor
methods should be "r" for a file
that is to be read-only or "rw" for
a file that is to be written (and perhaps read as well). In Java 1.4
and later, two other values for the mode
argument are allowed as well. A mode of
"rwd" opens the file for reading
and writing, and requires that (if the file resides on a local
filesystem) every update to the file content be written synchronously
to the underlying file. The "rws"
mode is similar, but requires synchronous updates to both the
file's content and its metadata (which includes
things such as file access times). Using
"rws" mode may require that the
file metadata be modified every time the file is read.
In Java 1.4 and later, use the getChannel(
)
method to obtain a
FileChannel object that you can use to access the
file using the New I/O API of java.nio and its
subpackages. If the RandomAccessFile was opened
with a mode of "r", the
FileChannel allows only reading. Otherwise, it
allows both reading and writing.
public class
RandomAccessFile implements Closeable, DataInput, DataOutput {
// Public Constructors
public
RandomAccessFile (File
file , String
mode ) throws FileNotFoundException;
public
RandomAccessFile (String
name , String
mode ) throws FileNotFoundException;
// Public Instance Methods
public void
close ( ) throws IOException; Implements:Closeable
1.4 public final java.nio.channels.FileChannel
getChannel ( );
public final FileDescriptor
getFD ( ) throws IOException;
public long
getFilePointer ( ) throws IOException; native
public long
length ( ) throws IOException; native
public int
read ( ) throws IOException; native
public int
read (byte[ ]
b ) throws IOException;
public int
read (byte[ ]
b , int
off , int
len ) throws IOException;
public void
seek (long
pos ) throws IOException; native
1.2 public void
setLength (long
newLength ) throws IOException; native
// Methods Implementing Closeable
public void
close ( ) throws IOException;
// Methods Implementing DataInput
public final boolean
readBoolean ( ) throws IOException;
public final byte
readByte ( ) throws IOException;
public final char
readChar ( ) throws IOException;
public final double
readDouble ( ) throws IOException;
public final float
readFloat ( ) throws IOException;
public final void
readFully (byte[ ]
b ) throws IOException;
public final void
readFully (byte[ ]
b , int
off , int
len ) throws IOException;
public final int
readInt ( ) throws IOException;
public final String
readLine ( ) throws IOException;
public final long
readLong ( ) throws IOException;
public final short
readShort ( ) throws IOException;
public final int
readUnsignedByte ( ) throws IOException;
public final int
readUnsignedShort ( ) throws IOException;
public final String
readUTF ( ) throws IOException;
public int
skipBytes (int
n ) throws IOException;
// Methods Implementing DataOutput
public void
write (int
b ) throws IOException; native
public void
write (byte[ ]
b ) throws IOException;
public void
write (byte[ ]
b , int
off , int
len ) throws IOException;
public final void
writeBoolean (boolean
v ) throws IOException;
public final void
writeByte (int
v ) throws IOException;
public final void
writeBytes (String
s ) throws IOException;
public final void
writeChar (int
v ) throws IOException;
public final void
writeChars (String
s ) throws IOException;
public final void
writeDouble (double
v ) throws IOException;
public final void
writeFloat (float
v ) throws IOException;
public final void
writeInt (int
v ) throws IOException;
public final void
writeLong (long
v ) throws IOException;
public final void
writeShort (int
v ) throws IOException;
public final void
writeUTF (String
str ) throws IOException;
}