RandomAccessFile in Java

The RandomAccessFile class provides a mechanism to read from and write to a file in a random-access manner. It allows you to directly access any position within the file, enabling both sequential and non-sequential operations. This class is particularly useful when you need to perform read and write operations at specific positions within a file without having to read or write the entire file sequentially.

To use the RandomAccessFile class, you first need to create an instance of it by providing the file path and the access mode as parameters to the constructor. The access mode can be either "r" for reading only, "rw" for reading and writing, "rws" for reading and writing with file synchronization, or "rwd" for reading and writing with file synchronization on metadata changes.

Moving Around a RandomAccessFile

The RandomAccessFile class provides the functionality to navigate to a specific position within a file using the seek() method. Once the file pointer is positioned, the class allows for reading and writing data through the use of the DataInput and DataOutput interfaces. These interfaces ensure that data can be processed in a manner that is independent of the underlying platform, promoting portability.

To determine the current position of the file pointer, one can utilize the getFilePointer() method, which returns the offset from the beginning of the file. This enables precise tracking of the location within the file during read and write operations, facilitating efficient data manipulation.

RandomAccessFile(File file, String mode)

The above constructor creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.

Access Mode

In the RandomAccessFile class, the default mode during instantiation is set to read-only, meaning that the file can only be accessed for reading operations. However, it is possible to specify different modes when creating an instance of RandomAccessFile. These modes determine the access permissions and behavior of the file.

The available modes in RandomAccessFile are as follows:

  1. "r" (Read-only mode): This mode allows only reading operations on the file. Any attempt to write to the file will result in a java.io.IOException.
  2. "rw" (Read-write mode): This mode enables both reading and writing operations on the file. It provides full access to read from and write to the file.
  3. "rws" (Read-write mode with file synchronization): This mode behaves similarly to "rw" mode, but with the additional feature of file synchronization. It ensures that file modifications are immediately synchronized with the underlying storage device.
  4. "rwd" (Read-write mode with file synchronization on metadata changes): This mode is similar to "rws" mode, but it synchronizes file changes primarily on metadata modifications, such as file length changes or last modified timestamp updates.

By specifying the appropriate mode, developers can control the level of file access and synchronization required for their specific use cases. It is essential to choose the mode that aligns with the desired file access permissions and the need for data consistency.

import java.util.*; import java.io.*; public class TestClass{ public static void main(String[] args) { try { RandomAccessFile raFile =new RandomAccessFile("D://test.txt","rw"); raFile.write("Java Tutorial".getBytes()); //add the content raFile.seek(raFile.getFilePointer()-8); //set pointer backward -8 characters raFile.write("File Class Tutorial ".getBytes()); //write the text where pointer is raFile.seek(0); //set pointer to start of file int i; while((i= raFile.read())!=-1){ System.out.print((char)i); } } catch (IOException e){ e.printStackTrace(); } } } //Output: Java File Class Tutorial


RandomAccessFile provides a powerful feature to directly access specific positions within a file, making it suitable for scenarios where random access is required, such as reading and modifying specific parts of a large file or implementing custom file formats.