Home » Java programming language

Java RandomAccessFile readFloat() Method with Example

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

RandomAccessFile Class readFloat() method

  • readFloat() method is available in java.io package.
  • readFloat() method is used to read float value from this RandomAccessFile and initially it takes int value by using the readInt() method and convert the int value to a float by using intBitsToFloat() method.
  • readFloat() 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.
  • readFloat() method may throw an exception at the time of reading float.
    • 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 4 bytes.

Syntax:

    public final float readFloat();

Parameter(s):

  • It does not accept any parameter.

Return value:

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

Example:

// Java program to demonstrate the example 
// of float readFloat() method of
// RandomAccessFile

import java.io.*;

class RAFReadFloat {
 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 writeFloat() method is to 
  // write float to the file
  ra_f.writeFloat(10025.358 f);

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

  // By using readFloat() method is to 
  // read float from the file

  float f = ra_f.readFloat();
  System.out.println("ra_f.readFloat(): " + f);

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

Output

ra_f.readFloat(): 1.0947689E-21


Comments and Discussions!

Load comments ↻





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