Random Access Files

Random access files can be read from or written to or both from a particular byte position in the file. The position in the file is indicated by a file pointer.

There are two constructors in this class:

 public RandomAccessFile(String name, String mode) throws IOException
 public RandomAccessFile(File file, String mode) throws IOException
The first argument to the constructor is the file you want to access. The second argument is the mode for access. This should either be the String "r" for read-only access or the string "rw" for read and write access. Java does not support write only access. For example,

RandomAccessFile raf = new RandomAccessFile("29.html", "r");
The getFilePointer(), length(), and seek() methods allow you to determine and modify the point in the file at which reads and writes occur. Attempts to seek (position the file pointer) past the end of the file just move the file pointer to the end of the file. Attempts to write from the end of the file extend the file. Attempts to read from the end of the file throw EOFExceptions.

 public native long getFilePointer() throws IOException
 public native void seek(long pos) throws IOException
 public native long length() throws IOException
Reads and writes use methods that work identically to the methods of the DataInputStream and DataOutputStream classes, except that you can set the position at which the read or write occurs between calls to the read and write methods.

 public native int read() throws IOException
 public int read(byte[] input, int offset, int length) throws IOException
 public int read(byte[] input) throws IOException
 public final void readFully(byte[] input) throws IOException
 public final void readFully(byte[] input, int offset, int length) 
  throws IOException
 public native void write(int b) throws IOException
 public void write(byte[] data) throws IOException
 public void write(byte[] data, int offset, int length) throws IOException
 public final boolean readBoolean() throws IOException
 public final byte readByte() throws IOException
 public final int readUnsignedByte() throws IOException
 public final short readShort() throws IOException
 public final int readUnsignedShort() throws IOException
 public final char readChar() throws IOException
 public final int readInt() throws IOException
 public final long readLong() throws IOException
 public final float readFloat() throws IOException
 public final double readDouble() throws IOException
 public final String readLine() throws IOException
 public final String readUTF() throws IOException
 public final void writeBoolean(boolean b) throws IOException
 public final void writeByte(int b) throws IOException
 public final void writeShort(int s) throws IOException
 public final void writeChar(int c) throws IOException
 public final void writeInt(int i) throws IOException
 public final void writeLong(long l) throws IOException
 public final void writeFloat(float f) throws IOException
 public final void writeDouble(double d) throws IOException
 public final void writeBytes(String s) throws IOException
 public final void writeChars(String s) throws IOException
 public final void writeUTF(String s) throws IOException
Finally, they're a few miscellaneous methods:

 public final FileDescriptor getFD() throws IOException
 public int skipBytes(int n) throws IOException
 public native void close() throws IOException

Previous | Next | Top
Last Modified July 6, 1999
Copyright 1997, 1999 Elliotte Rusty Harold
elharo@metalab.unc.edu