java.lang.Object | |
↳ | java.io.RandomAccessFile |
Allows reading from and writing to a file in a random-access manner. This is
different from the uni-directional sequential access that a
FileInputStream
or FileOutputStream
provides. If the file is
opened in read/write mode, write operations are available as well. The
position of the next read or write operation can be moved forwards and
backwards after every operation.
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new
RandomAccessFile based on file and opens
it according to the access string in mode . | |||||||||||
Constructs a new
RandomAccessFile based on the file named fileName and opens it according to the access string in mode . |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Closes this file.
| |||||||||||
Gets this file's
FileChannel object. | |||||||||||
Gets this file's
FileDescriptor . | |||||||||||
Gets the current position within this file.
| |||||||||||
Returns the length of this file in bytes.
| |||||||||||
Reads at most
count bytes from the current position in this file
and stores them in the byte array buffer starting at offset . | |||||||||||
Reads bytes from the current position in this file and stores them in the
byte array
buffer . | |||||||||||
Reads a single byte from the current position in this file and returns it
as an integer in the range from 0 to 255.
| |||||||||||
Reads a boolean from the current position in this file.
| |||||||||||
Reads an 8-bit byte from the current position in this file.
| |||||||||||
Reads a big-endian 16-bit character from the current position in this file.
| |||||||||||
Reads a big-endian 64-bit double from the current position in this file.
| |||||||||||
Reads a big-endian 32-bit float from the current position in this file.
| |||||||||||
Equivalent to
readFully(dst, 0, dst.length); . | |||||||||||
Reads
byteCount bytes from this stream and stores them in the byte
array dst starting at offset . | |||||||||||
Reads a big-endian 32-bit integer from the current position in this file.
| |||||||||||
Reads a line of text form the current position in this file.
| |||||||||||
Reads a big-endian 64-bit long from the current position in this file.
| |||||||||||
Reads a big-endian 16-bit short from the current position in this file.
| |||||||||||
Reads a string that is encoded in
modified UTF-8 from
this file. | |||||||||||
Reads an unsigned 8-bit byte from the current position in this file and
returns it as an integer.
| |||||||||||
Reads an unsigned big-endian 16-bit short from the current position in this file and
returns it as an integer.
| |||||||||||
Moves this file's file pointer to a new position, from where following
read , write or skip operations are done. | |||||||||||
Sets the length of this file to
newLength . | |||||||||||
Skips over
count bytes in this file. | |||||||||||
Writes a byte to this file, starting at the current file pointer.
| |||||||||||
Writes
count bytes from the byte array buffer to this
file, starting at the current file pointer and using offset as
the first position within buffer to get bytes. | |||||||||||
Writes the entire contents of the byte array
buffer to this file,
starting at the current file pointer. | |||||||||||
Writes a boolean to this file as a single byte (1 for true, 0 for false), starting at the
current file pointer.
| |||||||||||
Writes an 8-bit byte to this file, starting at the current file pointer.
| |||||||||||
Writes the low order 8-bit bytes from a string to this file, starting at
the current file pointer.
| |||||||||||
Writes a big-endian 16-bit character to this file, starting at the current file
pointer.
| |||||||||||
Writes big-endian 16-bit characters from
str to this file, starting at the
current file pointer. | |||||||||||
Writes a big-endian 64-bit double to this file, starting at the current file
pointer.
| |||||||||||
Writes a big-endian 32-bit float to this file, starting at the current file pointer.
| |||||||||||
Writes a big-endian 32-bit integer to this file, starting at the current file
pointer.
| |||||||||||
Writes a big-endian 64-bit long to this file, starting at the current file
pointer.
| |||||||||||
Writes a big-endian 16-bit short to this file, starting at the current file
pointer.
| |||||||||||
Writes a string encoded with
modified UTF-8 to this
file, starting at the current file pointer. |
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Called before the object's memory is reclaimed by the VM.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
| |||||||||||
From interface
java.io.Closeable
| |||||||||||
From interface
java.io.DataInput
| |||||||||||
From interface
java.io.DataOutput
|
Constructs a new RandomAccessFile
based on file
and opens
it according to the access string in mode
.
mode
may have one of following values:
"r" |
The file is opened in read-only mode. An IOException is
thrown if any of the write methods is called. |
"rw" |
The file is opened for reading and writing. If the file does not exist, it will be created. |
"rws" |
The file is opened for reading and writing. Every change of the file's content or metadata must be written synchronously to the target device. |
"rwd" |
The file is opened for reading and writing. Every change of the file's content must be written synchronously to the target device. |
file | the file to open. |
---|---|
mode | the file access mode, either "r" , "rw" , "rws" or "rwd" . |
FileNotFoundException | if the file cannot be opened or created according to mode . |
---|---|
IllegalArgumentException | if mode is not "r" , "rw" , "rws" or "rwd" . |
SecurityException | if a SecurityManager is installed and it denies
access request according to mode . |
Constructs a new RandomAccessFile
based on the file named fileName
and opens it according to the access string in mode
.
The file path may be specified absolutely or relative to the system
property "user.dir"
.
fileName | the name of the file to open. |
---|---|
mode | the file access mode, either "r" , "rw" , "rws" or "rwd" . |
FileNotFoundException | if the file cannot be opened or created according to mode . |
---|---|
IllegalArgumentException | if mode is not "r" , "rw" , "rws" or "rwd" . |
SecurityException | if a SecurityManager is installed and it denies
access request according to mode . |
Closes this file.
IOException | if an error occurs while closing this file. |
---|
Gets this file's FileChannel
object.
The file channel's position
is the same
as this file's file pointer offset (see getFilePointer()
). Any
changes made to this file's file pointer offset are also visible in the
file channel's position and vice versa.
Gets this file's FileDescriptor
. This represents the operating
system resource for this random access file.
IOException | if an error occurs while getting the file descriptor of this file. |
---|
Gets the current position within this file. All reads and writes take place at the current file pointer position.
IOException | if an error occurs while getting the file pointer of this file. |
---|
Returns the length of this file in bytes.
IOException | if this file is closed or some other I/O error occurs. |
---|
Reads at most count
bytes from the current position in this file
and stores them in the byte array buffer
starting at offset
. Blocks until count
bytes have been read, the end of the
file is reached or an exception is thrown.
buffer | the array in which to store the bytes read from this file. |
---|---|
offset | the initial position in buffer to store the bytes read
from this file. |
count | the maximum number of bytes to store in buffer . |
IndexOutOfBoundsException | if offset < 0 or count < 0 , or if offset + count is greater than the size of buffer . |
---|---|
IOException | if this file is closed or another I/O error occurs. |
Reads bytes from the current position in this file and stores them in the
byte array buffer
. The maximum number of bytes read corresponds
to the size of buffer
. Blocks until at least one byte has been
read.
buffer | the byte array in which to store the bytes read. |
---|
IOException | if this file is closed or another I/O error occurs. |
---|
Reads a single byte from the current position in this file and returns it as an integer in the range from 0 to 255. Returns -1 if the end of the file has been reached. Blocks until one byte has been read, the end of the file is detected or an exception is thrown.
IOException | if this file is closed or another I/O error occurs. |
---|
Reads a boolean from the current position in this file. Blocks until one byte has been read, the end of the file is reached or an exception is thrown.
EOFException | if the end of this file is detected. |
---|---|
IOException | if this file is closed or another I/O error occurs. |
Reads an 8-bit byte from the current position in this file. Blocks until one byte has been read, the end of the file is reached or an exception is thrown.
EOFException | if the end of this file is detected. |
---|---|
IOException | if this file is closed or another I/O error occurs. |
Reads a big-endian 16-bit character from the current position in this file. Blocks until two bytes have been read, the end of the file is reached or an exception is thrown.
EOFException | if the end of this file is detected. |
---|---|
IOException | if this file is closed or another I/O error occurs. |
Reads a big-endian 64-bit double from the current position in this file. Blocks until eight bytes have been read, the end of the file is reached or an exception is thrown.
EOFException | if the end of this file is detected. |
---|---|
IOException | if this file is closed or another I/O error occurs. |
Reads a big-endian 32-bit float from the current position in this file. Blocks until four bytes have been read, the end of the file is reached or an exception is thrown.
EOFException | if the end of this file is detected. |
---|---|
IOException | if this file is closed or another I/O error occurs. |
Equivalent to readFully(dst, 0, dst.length);
.
IOException |
---|
Reads byteCount
bytes from this stream and stores them in the byte
array dst
starting at offset
. If byteCount
is zero, then this
method returns without reading any bytes. Otherwise, this method blocks until
byteCount
bytes have been read. If insufficient bytes are available,
EOFException
is thrown. If an I/O error occurs, IOException
is
thrown. When an exception is thrown, some bytes may have been consumed from the stream
and written into the array.
dst | the byte array into which the data is read. |
---|---|
offset | the offset in dst at which to store the bytes. |
byteCount | the number of bytes to read. |
EOFException | if the end of the source stream is reached before enough bytes have been read. |
---|---|
IndexOutOfBoundsException | if offset < 0 or byteCount < 0 , or
offset + byteCount > dst.length . |
IOException | if a problem occurs while reading from this stream. |
NullPointerException | if dst is null.
|
Reads a big-endian 32-bit integer from the current position in this file. Blocks until four bytes have been read, the end of the file is reached or an exception is thrown.
EOFException | if the end of this file is detected. |
---|---|
IOException | if this file is closed or another I/O error occurs. |
Reads a line of text form the current position in this file. A line is
represented by zero or more characters followed by '\n'
, '\r'
, "\r\n"
or the end of file marker. The string does not
include the line terminating sequence.
Blocks until a line terminating sequence has been read, the end of the file is reached or an exception is thrown.
null
if no characters have
been read before the end of the file has been reached.IOException | if this file is closed or another I/O error occurs. |
---|
Reads a big-endian 64-bit long from the current position in this file. Blocks until eight bytes have been read, the end of the file is reached or an exception is thrown.
EOFException | if the end of this file is detected. |
---|---|
IOException | if this file is closed or another I/O error occurs. |
Reads a big-endian 16-bit short from the current position in this file. Blocks until two bytes have been read, the end of the file is reached or an exception is thrown.
EOFException | if the end of this file is detected. |
---|---|
IOException | if this file is closed or another I/O error occurs. |
Reads a string that is encoded in modified UTF-8
from
this file. The number of bytes that must be read for the complete string
is determined by the first two bytes read from the file. Blocks until all
required bytes have been read, the end of the file is reached or an
exception is thrown.
modified UTF-8
from
this file.EOFException | if the end of this file is detected. |
---|---|
IOException | if this file is closed or another I/O error occurs. |
UTFDataFormatException | if the bytes read cannot be decoded into a character string. |
Reads an unsigned 8-bit byte from the current position in this file and returns it as an integer. Blocks until one byte has been read, the end of the file is reached or an exception is thrown.
EOFException | if the end of this file is detected. |
---|---|
IOException | if this file is closed or another I/O error occurs. |
Reads an unsigned big-endian 16-bit short from the current position in this file and returns it as an integer. Blocks until two bytes have been read, the end of the file is reached or an exception is thrown.
EOFException | if the end of this file is detected. |
---|---|
IOException | if this file is closed or another I/O error occurs. |
Moves this file's file pointer to a new position, from where following
read
, write
or skip
operations are done. The
position may be greater than the current length of the file, but the
file's length will only change if the moving of the pointer is followed
by a write
operation.
offset | the new file pointer position. |
---|
IOException | if this file is closed, pos < 0 or another I/O error
occurs.
|
---|
Sets the length of this file to newLength
. If the current file is
smaller, it is expanded but the contents from the previous end of the
file to the new end are undefined. The file is truncated if its current
size is bigger than newLength
. If the current file pointer
position is in the truncated part, it is set to the end of the file.
newLength | the new file length in bytes. |
---|
IllegalArgumentException | if newLength < 0 . |
---|---|
IOException | if this file is closed or another I/O error occurs. |
Skips over count
bytes in this file. Less than count
bytes are skipped if the end of the file is reached or an exception is
thrown during the operation. Nothing is done if count
is
negative.
count | the number of bytes to skip. |
---|
IOException | if this file is closed or another I/O error occurs. |
---|
Writes a byte to this file, starting at the current file pointer. Only
the least significant byte of the integer oneByte
is written.
oneByte | the byte to write to this file. |
---|
IOException | if this file is closed or another I/O error occurs. |
---|
Writes count
bytes from the byte array buffer
to this
file, starting at the current file pointer and using offset
as
the first position within buffer
to get bytes.
buffer | the buffer to write to this file. |
---|---|
offset | the index of the first byte in buffer to write. |
count | the number of bytes from buffer to write. |
IndexOutOfBoundsException | if count < 0 , offset < 0 or count +
offset is greater than the size of buffer . |
---|---|
IOException | if an I/O error occurs while writing to this file. |
Writes the entire contents of the byte array buffer
to this file,
starting at the current file pointer.
buffer | the buffer to write. |
---|
IOException | if an I/O error occurs while writing to this file. |
---|
Writes a boolean to this file as a single byte (1 for true, 0 for false), starting at the current file pointer.
val | the boolean to write to this file. |
---|
IOException | if this file is closed or another I/O error occurs. |
---|
Writes an 8-bit byte to this file, starting at the current file pointer.
Only the least significant byte of the integer val
is written.
val | the byte to write to this file. |
---|
IOException | if this file is closed or another I/O error occurs. |
---|
Writes the low order 8-bit bytes from a string to this file, starting at the current file pointer.
str | the string containing the bytes to write to this file |
---|
IOException | if an I/O error occurs while writing to this file. |
---|
Writes a big-endian 16-bit character to this file, starting at the current file
pointer. Only the two least significant bytes of the integer val
are written, with the high byte first.
val | the char to write to this file. |
---|
IOException | if an I/O error occurs while writing to this file. |
---|
Writes big-endian 16-bit characters from str
to this file, starting at the
current file pointer.
str | the string to write to this file. |
---|
IOException | if an I/O error occurs while writing to this file. |
---|
Writes a big-endian 64-bit double to this file, starting at the current file
pointer. The bytes are those returned by
doubleToLongBits(double)
, meaning a canonical NaN is used.
val | the double to write to this file. |
---|
IOException | if an I/O error occurs while writing to this file. |
---|
Writes a big-endian 32-bit float to this file, starting at the current file pointer.
The bytes are those returned by floatToIntBits(float)
, meaning a canonical NaN
is used.
val | the float to write to this file. |
---|
IOException | if an I/O error occurs while writing to this file. |
---|
Writes a big-endian 32-bit integer to this file, starting at the current file pointer.
val | the int to write to this file. |
---|
IOException | if an I/O error occurs while writing to this file. |
---|
Writes a big-endian 64-bit long to this file, starting at the current file pointer.
val | the long to write to this file. |
---|
IOException | if an I/O error occurs while writing to this file. |
---|
Writes a big-endian 16-bit short to this file, starting at the current file
pointer. Only the two least significant bytes of the integer val
are written.
val | the short to write to this file. |
---|
IOException | if an I/O error occurs while writing to this file. |
---|
Writes a string encoded with modified UTF-8
to this
file, starting at the current file pointer.
str | the string to write in modified UTF-8
format. |
---|
IOException | if an I/O error occurs while writing to this file. |
---|---|
UTFDataFormatException | if the encoded string is longer than 65535 bytes. |
Called before the object's memory is reclaimed by the VM. This can only happen once the garbage collector has detected that the object is no longer reachable by any thread of the running application.
The method can be used to free system resources or perform other cleanup
before the object is garbage collected. The default implementation of the
method is empty, which is also expected by the VM, but subclasses can
override finalize()
as required. Uncaught exceptions which are
thrown during the execution of this method cause it to terminate
immediately but are otherwise ignored.
Note that the VM does guarantee that finalize()
is called at most
once for any object, but it doesn't guarantee when (if at all) finalize()
will be called. For example, object B's finalize()
can delay the execution of object A's finalize()
method and
therefore it can delay the reclamation of A's memory. To be safe, use a
ReferenceQueue
, because it provides more control
over the way the VM deals with references during garbage collection.
Throwable |
---|