RandomAccessFile Introduction : RandomAccessFile « File « Java Tutorial






  1. A RandomAccessFile employs an internal pointer that points to the next byte to read.
  2. This pointer is zero-based and the first byte is indicated by index 0.
  3. When first created, a RandomAccessFile points to the first byte.
  4. You can change the pointer's position by invoking the seek method.
  5. The skipBytes method moves the pointer by the specified number of bytes.
  6. If skipping offset number of bytes would pass the end of file, the internal pointer will only move to as much as the end of file.
  7. The skipBytes method returns the actual number of bytes skipped.

When opening a file using a RandomAccessFile, you can choose whether to open it read-only or read write.

public RandomAccessFile (File file, String mode) throws FileNotFoundException
public RandomAccessFile (String filePath, String mode) throws FileNotFoundException

The value of mode can be one of these:

"r".     Open for reading only.

      "rw".    Open for reading and writing. 
               If the file does not already exist, RandomAccessFile creates the file.

      "rws".   Open for reading and writing and require that every update to the file's content and metadata be written synchronously.

      "rwd".   Open for reading and writing and require that every update to the file's content (but not metadata) be written synchronously.








11.40.RandomAccessFile
11.40.1.RandomAccessFile Introduction
11.40.2.Employs RandomAccessFile to store ints and changes the value of the third int.
11.40.3.Seek in RandomAccessFile
11.40.4.Getting FileChannel from RandomAccessFile
11.40.5.Write int to RandomAccessFile using FileChannel
11.40.6.Use RandomAccessFile to save and read
11.40.7.Use RandomAccessFile to reverse a file
11.40.8.Test file pointer manipulation between FileChannel and RandomAccessFile objects.
11.40.9.Appending data to existing file