Java RandomAccessFile class

Last Updated : 9 Feb 2026

The RandomAccessFile class is used to read and write data to a file at any position. It allows accessing a file like an array of bytes.

In this chapter, we will learn how to use the RandomAccessFile class to read and write data at any position in a file.

What is Java RandomAccessFile Class?

The RandomAccessFile class belongs to the java.io package and allows reading and writing data at any location in a file using a file pointer.

The file pointer moves automatically after read or write operations. If the end of the file is reached before reading the required bytes, an EOFException is thrown.

Java RandomAccessFile Class Declaration

The declaration of the RandomAccessFile class is as follows:

Constructors of RandomAccessFile Class

The RandomAccessFile class provides constructors to open a file in read or read-write mode.

1. RandomAccessFile(File file, String mode)

This constructor creates a random-access file stream to read from, and optionally to write to, the file specified by the File object.

Here is the syntax:

2. RandomAccessFile(String name, String mode)

This constructor creates a random-access file stream to read from, and optionally to write to, a file with the specified name.

Here is the syntax:

The mode can be:

  • "r": The read only mode.
  • "rw": The read and write mode.

Methods of RandomAccessFile Class

The RandomAccessFile class provides methods to read, write, and control file pointer position.

Constructor

ConstructorDescription
RandomAccessFile(File file, String mode)Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.
RandomAccessFile(String name, String mode)Creates a random access file stream to read from, and optionally to write to, a file with the specified name.

Method

Modifier and TypeMethodMethod
voidclose()It closes this random access file stream and releases any system resources associated with the stream.
FileChannelgetChannel()It returns the unique FileChannel object associated with this file.
intreadInt()It reads a signed 32-bit integer from this file.
StringreadUTF()It reads in a string from this file.
voidseek(long pos)It sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.
voidwriteDouble(double v)It converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the file as an eight-byte quantity, high byte first.
voidwriteFloat(float v)It converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the file as a four-byte quantity, high byte first.
voidwrite(int b)It writes the specified byte to this file.
intread()It reads a byte of data from this file.
longlength()It returns the length of this file.
voidseek(long pos)It sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.

Examples of RandomAccessFile Class

The following examples show how to use the RandomAccessFile class in Java.

Example 1: Read Data from File Using RandomAccessFile

In this example, data is read from a file starting from a specific position.

Output:

This class is used

Example 2: Write Data at Specific Position Using RandomAccessFile

In this example, text is written at a specific position in the file.

File Content After Execution:

This class is used for reading I love my country and my people.