java.io.RandomAccessFile
RandomAccessFile is a class which allows positioning of the next read
anywhere in the file. This is useful for reading specific locations of files
or following links within a file. Most input classes only support forward
skipping.
Summary
Public Constructors
Public Methods
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait
write,
write,
write,
writeBoolean,
writeByte,
writeBytes,
writeChar,
writeChars,
writeDouble,
writeFloat,
writeInt,
writeLong,
writeShort,
writeUTF
Details
Public Constructors
public
RandomAccessFile(File file, String mode)
Constructs a new RandomAccessFile on the File
file and
opens it according to the access String in
mode. The
access mode may be one of
"r" for read access only, or
"rw" for read/write access.
Parameters
| file
| the File to open. |
| mode
| "r" for read only, or "rw" for read/write. |
public
RandomAccessFile(String fileName, String mode)
Constructs a new RandomAccessFile on the file named
fileName
and opens it according to the access String in
mode. The
file may be absolute or relative to the System property
"user.dir". The access mode may be one of
"r" for read access only, or
"rw" for
read/write access.
Parameters
| fileName
| the filename of the file to open. |
| mode
| "r" for read only, or "rw" for read/write. |
Public Methods
public
void
close()
Close this RandomAccessFile.
Throws
| IOException
| If an error occurs attempting to close this RandomAccessFile.
|
public
final
synchronized
FileChannel
getChannel()
Returns the FileChannel equivalent to this stream.
The file channel is write-only and has an initial position within the
file that is the same as the current position of this FileOutputStream
within the file. All changes made to the underlying file descriptor state
via the channel are visible by the output stream and vice versa.
Returns
- the file channel representation for this FileOutputStream.
Returns the FileDescriptor representing the operating system resource for
this RandomAccessFile.
Returns
- the FileDescriptor for this RandomAccessFile.
Throws
| IOException
| If an error occurs attempting to get the FileDescriptor of
this RandomAccessFile.
|
public
long
getFilePointer()
Returns the current position within this RandomAccessFile. All reads and
writes take place at the current file pointer position.
Returns
- the current file pointer position.
Throws
| IOException
| If an error occurs attempting to get the file pointer
position of this RandomAccessFile.
|
public
long
length()
Returns the current length of this RandomAccessFile in bytes.
Returns
- the current file length in bytes.
Throws
| IOException
| If an error occurs attempting to get the file length of this
RandomAccessFile.
|
public
int
read(byte[] buffer, int offset, int count)
Reads at most
count bytes from this RandomAccessFile and
stores them in byte array
buffer starting at
offset. Answer the number of bytes actually read or -1 if
no bytes were read and end of file was encountered.
Parameters
| buffer
| the byte array in which to store the read bytes. |
| offset
| the offset in buffer to store the read bytes. |
| count
| the maximum number of bytes to store in buffer. |
Returns
- the number of bytes actually read or -1 if end of file.
Throws
| IOException
| If an error occurs attempting to read from this
RandomAccessFile. |
public
int
read()
Reads a single byte from this RandomAccessFile and returns the result as
an int. The low-order byte is returned or -1 of the end of file was
encountered.
Returns
- the byte read or -1 if end of file.
Throws
| IOException
| If an error occurs attempting to read from this
RandomAccessFile. |
public
int
read(byte[] buffer)
Reads bytes from this RandomAccessFile into the byte array
buffer. The number of bytes actually read is returned.
Parameters
| buffer
| the buffer to read bytes into |
Returns
- the number of bytes actually read or -1 if end of file.
Throws
| IOException
| If an error occurs attempting to read from this
RandomAccessFile. |
public
final
boolean
readBoolean()
Reads a boolean from this stream.
Returns
- boolean the next boolean value from the source stream.
Throws
| IOException
| If a problem occurs reading from this DataInputStream. |
public
final
byte
readByte()
Reads an 8-bit byte value from this stream.
Returns
- byte the next byte value from the source stream.
Throws
| IOException
| If a problem occurs reading from this DataInputStream. |
public
final
char
readChar()
Reads a 16-bit character value from this stream.
Returns
- char the next
char value from the source stream.
Throws
| IOException
| If a problem occurs reading from this DataInputStream. |
public
final
double
readDouble()
Reads a 64-bit
double value from this stream.
Returns
- double the next
double value from the source
stream.
Throws
| IOException
| If a problem occurs reading from this DataInputStream. |
public
final
float
readFloat()
Reads a 32-bit
float value from this stream.
Returns
- float the next
float value from the source stream.
Throws
| IOException
| If a problem occurs reading from this DataInputStream. |
public
final
void
readFully(byte[] buffer, int offset, int count)
Read bytes from this stream and stores them in byte array
buffer starting at offset
offset. This
method blocks until
count number of bytes have been read.
Parameters
| buffer
| the byte array in which to store the read bytes. |
| offset
| the offset in buffer to store the read bytes. |
| count
| the maximum number of bytes to store in buffer. |
Throws
| IOException
| If a problem occurs reading from this DataInputStream. |
public
final
void
readFully(byte[] buffer)
Reads bytes from this stream into the byte array
buffer.
This method will block until
buffer.length number of bytes
have been read.
Parameters
| buffer
| the buffer to read bytes into |
Throws
| IOException
| If a problem occurs reading from this DataInputStream. |
public
final
int
readInt()
Reads a 32-bit integer value from this stream.
Returns
- int the next
int value from the source stream.
Throws
| IOException
| If a problem occurs reading from this DataInputStream. |
public
final
String
readLine()
Returns a
String representing the next line of text
available in this BufferedReader. A line is represented by 0 or more
characters followed by
'\n',
'\r',
"\n\r" or end of stream. The
String does
not include the newline sequence.
Returns
- String the contents of the line or null if no characters were
read before end of stream.
Throws
| IOException
| If the BufferedReader is already closed or some other IO
error occurs.
|
public
final
long
readLong()
Reads a 64-bit
long value from this stream.
Returns
- long the next
long value from the source stream.
Throws
| IOException
| If a problem occurs reading from this DataInputStream. |
public
final
short
readShort()
Reads a 16-bit
short value from this stream.
Returns
- short the next
short value from the source stream.
Throws
| IOException
| If a problem occurs reading from this DataInputStream. |
public
final
String
readUTF()
Reads a UTF format String from this Stream.
Returns
- String the next UTF String from the source stream.
Throws
| IOException
| If a problem occurs reading from this DataInputStream. |
public
final
int
readUnsignedByte()
Reads an unsigned 8-bit
byte value from this stream and
returns it as an int.
Returns
- int the next unsigned byte value from the source stream.
Throws
| IOException
| If a problem occurs reading from this DataInputStream. |
public
final
int
readUnsignedShort()
Reads a 16-bit unsigned
short value from this stream and
returns it as an int.
Returns
- int the next unsigned
short value from the source
stream.
Throws
| IOException
| If a problem occurs reading from this DataInputStream. |
public
void
seek(long pos)
Seeks to the position
pos in this RandomAccessFile. All
read/write/skip methods sent will be relative to
pos.
Parameters
| pos
| the desired file pointer position |
Throws
| IOException
| If the stream is already closed or another IOException
occurs.
|
public
void
setLength(long newLength)
Set the length of this file to be
newLength. If the
current file is smaller, it will be expanded and the filePosition will be
set to the new file length. If the
newLength is smaller
then the file will be truncated.
Parameters
| newLength
| the desired file length |
Throws
| IOException
| If the stream is already closed or another IOException
occurs.
|
public
int
skipBytes(int count)
Skips
count number of bytes in this stream. Subsequent
read()'s will not return these bytes unless
reset() is used.
Parameters
| count
| the number of bytes to skip. |
Returns
- the number of bytes actually skipped.
Throws
| IOException
| If the stream is already closed or another IOException
occurs.
|
public
void
write(byte[] buffer, int offset, int count)
Writes
count bytes from the byte array
buffer
starting at
offset to this RandomAccessFile starting at
the current file pointer..
Parameters
| buffer
| the bytes to be written |
| offset
| offset in buffer to get bytes |
| count
| number of bytes in buffer to write |
public
void
write(int oneByte)
Writes the specified byte
oneByte to this RandomAccessFile
starting at the current file pointer. Only the low order byte of
oneByte is written.
Parameters
| oneByte
| the byte to be written |
Throws
| IOException
| If an error occurs attempting to write to this
RandomAccessFile. |
public
void
write(byte[] buffer)
Writes the entire contents of the byte array
buffer to
this RandomAccessFile starting at the current file pointer.
Parameters
| buffer
| the buffer to be written. |
Throws
| IOException
| If an error occurs trying to write to this RandomAccessFile. |
public
final
void
writeBoolean(boolean val)
Writes a boolean to this output stream.
Parameters
| val
| the boolean value to write to the OutputStream |
Throws
| IOException
| If an error occurs attempting to write to this
DataOutputStream. |
public
final
void
writeByte(int val)
Writes a 8-bit byte to this output stream.
Parameters
| val
| the byte value to write to the OutputStream |
Throws
| IOException
| If an error occurs attempting to write to this
DataOutputStream. |
public
final
void
writeBytes(String str)
Writes the low order 8-bit bytes from a String to this output stream.
Parameters
| str
| the String containing the bytes to write to the OutputStream |
Throws
| IOException
| If an error occurs attempting to write to this
DataOutputStream. |
public
final
void
writeChar(int val)
Writes the specified 16-bit character to the OutputStream. Only the lower
2 bytes are written with the higher of the 2 bytes written first. This
represents the Unicode value of val.
Parameters
| val
| the character to be written |
Throws
| IOException
| If an error occurs attempting to write to this
DataOutputStream. |
public
final
void
writeChars(String str)
Writes the specified 16-bit characters contained in str to the
OutputStream. Only the lower 2 bytes of each character are written with
the higher of the 2 bytes written first. This represents the Unicode
value of each character in str.
Parameters
| str
| the String whose characters are to be written. |
Throws
| IOException
| If an error occurs attempting to write to this
DataOutputStream. |
public
final
void
writeDouble(double val)
Writes a 64-bit double to this output stream. The resulting output is the
8 bytes resulting from calling Double.doubleToLongBits().
Parameters
| val
| the double to be written. |
Throws
| IOException
| If an error occurs attempting to write to this
DataOutputStream. |
public
final
void
writeFloat(float val)
Writes a 32-bit float to this output stream. The resulting output is the
4 bytes resulting from calling Float.floatToIntBits().
Parameters
| val
| the float to be written. |
Throws
| IOException
| If an error occurs attempting to write to this
DataOutputStream. |
public
final
void
writeInt(int val)
Writes a 32-bit int to this output stream. The resulting output is the 4
bytes, highest order first, of val.
Parameters
| val
| the int to be written. |
Throws
| IOException
| If an error occurs attempting to write to this
DataOutputStream. |
public
final
void
writeLong(long val)
Writes a 64-bit long to this output stream. The resulting output is the 8
bytes, highest order first, of val.
Parameters
| val
| the long to be written. |
Throws
| IOException
| If an error occurs attempting to write to this
DataOutputStream. |
public
final
void
writeShort(int val)
Writes the specified 16-bit short to the OutputStream. Only the lower 2
bytes are written with the higher of the 2 bytes written first.
Parameters
| val
| the short to be written |
Throws
| IOException
| If an error occurs attempting to write to this
DataOutputStream. |
public
final
void
writeUTF(String str)
Writes the specified String out in UTF format.
Parameters
| str
| the String to be written in UTF format. |
Throws
| IOException
| If an error occurs attempting to write to this
DataOutputStream. |