Home » Java programming language

Java RandomAccessFile readLong() Method with Example

RandomAccessFile Class readLong() method: Here, we are going to learn about the readLong() method of RandomAccessFile Class with its syntax and example.
Submitted by Preeti Jain, on March 23, 2020

RandomAccessFile Class readLong() method

  • readLong() method is available in java.io package.
  • readLong() method is used to read signed 64-bit long integer value from this RandomAccessFile.
  • readLong() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • readLong() method may throw an exception at the time of reading long integer value.
    • IOException: This exception may throw an exception while performing input/output operation.
    • EOFException: This exception may throw when the file pointer reaches EOF (End-Of-File) before reading 8 bytes.

Syntax:

    public final long readLong();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is long, it returns the 8 bytes of data from this RandomAccessFile manipulated as a long integer value.

Example:

// Java program to demonstrate the example 
// of long readLong() method of
// RandomAccessFile

import java.io.*;

class RAFReadLong {
 public static void main(String[] args) throws Exception {
  // Instantiate a random access file
  // object with file name and permissions
  RandomAccessFile ra_f = new RandomAccessFile("e:/includehelp.txt", "rw");

  // By using writeLong() method is to 
  // write long to the file
  ra_f.writeLong(10202456 l);

  // Initially set the file pointer
  // is at 1 for reading the file
  ra_f.seek(1);

  // By using readLong() method is to 
  // read a long value up to signed 64 bit
  // from this file

  long l = ra_f.readLong();
  System.out.println("ra_f.readLong(): " + l);

  // By using close() method isto
  // close this stream ran_f
  ra_f.close();
 }
}

Output

ra_f.readLong(): 2611828837



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.