Java - RandomAccessFile writeLong(long v) method



Description

The Java RandomAccessFile writeLong(long v) method writes a long to the file as eight bytes, high byte first. The write starts at the current position of the file pointer.

writeLong(long v) method −

  • Writes a 64-bit (8-byte) long value to the file.

  • The bytes are written in big-endian order (most significant byte first).

  • The file pointer moves forward by 8 bytes.

  • You can read it back using readLong().

Declaration

Following is the declaration for java.io.RandomAccessFile.writeLong(long v) method.

public final void writeLong(long v)

Parameters

v − a long value to be written.

Return Value

This method does not return a value.

Exception

  • IOException − If an I/O error occurs.

Example - Usage of RandomAccessFile writeLong(long v) method

The following example shows the usage of RandomAccessFile writeLong(long v) 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 f = 184750874576l;

         // create a new RandomAccessFile with filename test
         RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");

         // write a long in the file
         raf.writeLong(f);

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

         // read long
         System.out.println(raf.readLong());

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

         // write a long at the start
         raf.writeLong(20000000000l);

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

         // read 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 −

184750874576
20000000000

Example - Writing and Reading a Single Long Value

The following example shows the usage of RandomAccessFile writeLong(long v) 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("long1.dat", "rw")) {
         // Write a long value
         raf.writeLong(9876543210L);

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

         // Read the long value back
         long value = raf.readLong();
         System.out.println("Read long: " + value);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Read long: 9876543210

Explanation

  • 9876543210L is stored as 8 bytes.

  • File content: 8 bytes in big-endian representing 9876543210.

  • readLong() reads exactly what was written.

Example - Writing Multiple Long Values and Reading Them

The following example shows the usage of RandomAccessFile writeLong(long v) 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("long2.dat", "rw")) {
         // Write multiple long values
         raf.writeLong(10000000000L);
         raf.writeLong(20000000000L);
         raf.writeLong(30000000000L);

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

         // Read back the long values
         long a = raf.readLong();
         long b = raf.readLong();
         long c = raf.readLong();

         System.out.println("Read values: " + a + ", " + b + ", " + c);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Read values: 10000000000, 20000000000, 30000000000

Explanation

  • Writes three long values (8 bytes each) → total 24 bytes.

  • seek(0) resets the file pointer to start.

  • readLong() reads them in the same order.

java_io_randomaccessfile.htm
Advertisements