Java - RandomAccessFile readLong() method



Description

The Java RandomAccessFile readLong() method reads a signed 64-bit integer from this file. This method reads eight bytes from the file, starting at the current file pointer.

Declaration

Following is the declaration for java.io.RandomAccessFile.readLong() method.

public final long readLong()

Parameters

NA

Return Value

This method returns the next eight bytes of this file, interpreted as a long.

Exception

  • IOException − If an I/O error occurs.

  • EOFException − If this file reaches the end before reading eight bytes.

Example - Usage of RandomAccessFile readLong() method

The following example shows the usage of RandomAccessFile readLong() method.

RandomAccessFileDemo.java

package com.tutorialspoint;

import java.io.RandomAccessFile;
import java.io.IOException;

public class RandomAccessFileDemo {
   public static void main(String[] args) {

      try {
         long l = 536475859696l;
         
         // create a new RandomAccessFile with filename test
         RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");

         // write something in the file
         raf.writeLong(l);

         // set the file pointer at 0 position
         raf.seek(0);

         // print the long
         System.out.println(raf.readLong());

         // set the file pointer at 0 position
         raf.seek(0);

         // write something in the file
         raf.writeLong(4876347485l);

         raf.seek(0);
         // print the long
         System.out.println(raf.readLong());
         
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

Assuming we have a text file test.txt in current directory which has the following content. This file will be used as an input for our example program −

ABCDE

Let us compile and run the above program, this will produce the following result −

536475859696
4876347485

Example - Writing and Reading Long Values

The following example shows the usage of RandomAccessFile readLong() method.

RandomAccessFileDemo.java

package com.tutorialspoint;

import java.io.RandomAccessFile;
import java.io.IOException;

public class RandomAccessFileDemo {
   public static void main(String[] args) {
      try {
         RandomAccessFile raf = new RandomAccessFile("longs1.dat", "rw");

         // Write two long values (each takes 8 bytes)
         raf.writeLong(10000000000L);  // 10 billion
         raf.writeLong(20000000000L);  // 20 billion

         // Reset pointer to the beginning
         raf.seek(0);

         // Read long values
         long first = raf.readLong();
         long second = raf.readLong();

         System.out.println("First long: " + first);   // 10000000000
         System.out.println("Second long: " + second); // 20000000000

         raf.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

First long: 10000000000
Second long: 20000000000

Explanation

  • writeLong(long l) writes 8 bytes to store a long value.

  • readLong() reads 8 bytes and returns the long.

  • Used when storing and retrieving large integer values, like timestamps or IDs.

Example - Random Access to a Specific Long

The following example shows the usage of RandomAccessFile readLong() method.

RandomAccessFileDemo.java

package com.tutorialspoint;

import java.io.RandomAccessFile;
import java.io.IOException;

public class RandomAccessFileDemo {
   public static void main(String[] args) {
      try {
         RandomAccessFile raf = new RandomAccessFile("longs2.dat", "rw");

         // Write 3 long values
         raf.writeLong(111L); // Position 0
         raf.writeLong(222L); // Position 8
         raf.writeLong(333L); // Position 16

         // Jump to the third long (offset = 2 × 8 = 16 bytes)
         raf.seek(16);
         long third = raf.readLong();

         System.out.println("Third long: " + third); // 333

         raf.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Third long: 333

Explanation

  • Each long occupies 8 bytes.

  • seek(16) jumps to the third long value directly.

  • readLong() reads the value at that position.

  • This demonstrates random access − useful for databases, logs, or index files.

java_io_randomaccessfile.htm