RandomAccessFile

Instances of this class support both reading and writing to a random access file. A random access file behaves like a large array of bytes stored in the file system. There is a kind of cursor, or index into the implied array, called the file pointer; input operations read bytes starting at the file pointer and advance the file pointer past the bytes read. If the random access file is created in read/write mode, then output operations are also available; output operations write bytes starting at the file pointer and advance the file pointer past the bytes written. Output operations that write past the current end of the implied array cause the array to be extended. The file pointer can be read by the getFilePointer() method and set by the seek() method.

Here are some of the methods that you may need:

The following code shows how to use the RandomAccessFile class. The random access file in this example is a binary file called accounts.


import java.io.*;

public class RandomFile
{
  final static int LONG_SIZE = 8;
  final static int DOUBLE_SIZE = 8;
  final static int RECORD_SIZE = LONG_SIZE + DOUBLE_SIZE;

  static RandomAccessFile file;
  static long numAccounts = 0;
  
  public static void updateHeader ( double newBalance ) throws IOException
  {
    file.seek ( 0 );
    file.writeLong ( numAccounts );

    double tmpBal;
    if ( numAccounts == 0 )
      tmpBal = newBalance;
    else
    {
      tmpBal = file.readDouble ();
      tmpBal += newBalance;
    }
    file.seek ( LONG_SIZE );
    file.writeDouble ( tmpBal );
  }
  
  public static void writeHeader ( ) throws IOException
  {
    file.seek ( 0 );
    System.out.println ( "The number of accounts = " + file.readLong() );
    System.out.println ( "The total balance = " + file.readDouble() );
  }

  public static void createAccount ( double newBalance ) throws IOException
  {
    numAccounts++;
    file.seek ( numAccounts * RECORD_SIZE );
    file.writeLong ( numAccounts );
    file.writeDouble ( newBalance );
    updateHeader ( newBalance );
  }

  public static int fileSize ( ) throws IOException
  {
    return ( int ) ( file.length() / RECORD_SIZE );
  }

  public static void scanFile () throws IOException
  {
    file.seek ( 0 );
    long numRecords = file.readLong ();
    double sumBalance = file.readDouble ();

    long account;
    double balance;

    for (long i = 1; i <= numRecords; i++ )
    {
      account = file.readLong ();
      balance = file.readDouble ();
      System.out.println ("Account number: " + account + "  " +
			  "Balance: " + balance );
    }
  }

  public static void main ( String args[] ) throws IOException
  {
    // Create random access file and update header
    file = new RandomAccessFile ("accounts", "rw");
    double sumBalance = 0.0;
    updateHeader ( sumBalance );

    // Create account and update header
    double newBalance = 1000.0;
    createAccount ( newBalance );

    // Create another account and update header
    newBalance = 2000.0;
    createAccount ( newBalance );

    // Create another account and update header
    newBalance = 3000.0;
    createAccount ( newBalance );

    // Write out the size of the file
    System.out.println ( "The number of records in the file is " + fileSize( ) );

    // Write out the header of the file
    writeHeader ();

    // Scan the file
    scanFile ();
  }
}