RandomAccessFile

public class RandomAccessFile extends Object
implements DataInput DataOutput Closeable

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 Constructor Summary

RandomAccessFile(File file, String mode)
Constructs a new RandomAccessFile based on file and opens it according to the access string in mode.
RandomAccessFile(String fileName, String mode)
Constructs a new RandomAccessFile based on the file named fileName and opens it according to the access string in mode.

Public Method Summary

void
close()
Closes this file.
synchronized final FileChannel
getChannel()
Gets this file's FileChannel object.
final FileDescriptor
getFD()
Gets this file's FileDescriptor.
long
getFilePointer()
Gets the current position within this file.
long
length()
Returns the length of this file in bytes.
int
read()
Reads a single byte from the current position in this file and returns it as an integer in the range from 0 to 255.
int
read(byte[] buffer)
Reads bytes from the current position in this file and stores them in the byte array buffer.
int
read(byte[] buffer, int byteOffset, int byteCount)
Reads up to byteCount bytes from the current position in this file and stores them in the byte array buffer starting at byteOffset.
final boolean
readBoolean()
Reads a boolean from the current position in this file.
final byte
readByte()
Reads an 8-bit byte from the current position in this file.
final char
readChar()
Reads a big-endian 16-bit character from the current position in this file.
final double
readDouble()
Reads a big-endian 64-bit double from the current position in this file.
final float
readFloat()
Reads a big-endian 32-bit float from the current position in this file.
final void
readFully(byte[] dst)
Equivalent to readFully(dst, 0, dst.length);.
final void
readFully(byte[] dst, int offset, int byteCount)
Reads byteCount bytes from this stream and stores them in the byte array dst starting at offset.
final int
readInt()
Reads a big-endian 32-bit integer from the current position in this file.
final String
readLine()
Reads a line of text form the current position in this file.
final long
readLong()
Reads a big-endian 64-bit long from the current position in this file.
final short
readShort()
Reads a big-endian 16-bit short from the current position in this file.
final String
readUTF()
Reads a string that is encoded in modified UTF-8 from this file.
final int
readUnsignedByte()
Reads an unsigned 8-bit byte from the current position in this file and returns it as an integer.
final int
readUnsignedShort()
Reads an unsigned big-endian 16-bit short from the current position in this file and returns it as an integer.
void
seek(long offset)
Moves this file's file pointer to a new position, from where following read, write or skip operations are done.
void
setLength(long newLength)
Sets the length of this file to newLength.
int
skipBytes(int count)
Skips over count bytes in this file.
void
write(byte[] buffer)
Writes the entire contents of the byte array buffer to this file, starting at the current file pointer.
void
write(byte[] buffer, int byteOffset, int byteCount)
Writes byteCount bytes from the byte array buffer to this file, starting at the current file pointer and using byteOffset as the first position within buffer to get bytes.
void
write(int oneByte)
Writes a byte to this file, starting at the current file pointer.
final void
writeBoolean(boolean val)
Writes a boolean to this file as a single byte (1 for true, 0 for false), starting at the current file pointer.
final void
writeByte(int val)
Writes an 8-bit byte to this file, starting at the current file pointer.
final void
writeBytes(String str)
Writes the low order 8-bit bytes from a string to this file, starting at the current file pointer.
final void
writeChar(int val)
Writes a big-endian 16-bit character to this file, starting at the current file pointer.
final void
writeChars(String str)
Writes big-endian 16-bit characters from str to this file, starting at the current file pointer.
final void
writeDouble(double val)
Writes a big-endian 64-bit double to this file, starting at the current file pointer.
final void
writeFloat(float val)
Writes a big-endian 32-bit float to this file, starting at the current file pointer.
final void
writeInt(int val)
Writes a big-endian 32-bit integer to this file, starting at the current file pointer.
final void
writeLong(long val)
Writes a big-endian 64-bit long to this file, starting at the current file pointer.
final void
writeShort(int val)
Writes a big-endian 16-bit short to this file, starting at the current file pointer.
final void
writeUTF(String str)
Writes a string encoded with modified UTF-8 to this file, starting at the current file pointer.