Java - ObjectInputStream readFloat() method



Description

The Java ObjectInputStream readFloat() method reads a single float (32-bit floating-point number) from the input stream. It is used when a float value has been written using writeFloat(float f).

Declaration

Following is the declaration for java.io.ObjectInputStream.readFloat() method −

public float readFloat()

Parameters

NA

Return Value

This method returns the 32 bit float read.

Exception

  • EOFException − If end of file is reached.

  • IOException − If an I/O error has occurred.

Example - Usage of ObjectInputStream readFloat() method

The following example shows the usage of Java ObjectInputStream readFloat() method.

ObjectInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectInputStreamDemo {
   public static void main(String[] args) {
      float f = 1.198743f;
      
      try {
         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStream oout = new ObjectOutputStream(out);

         // write something in the file
         oout.writeFloat(f);
         oout.writeFloat(5973.9834f);
         oout.flush();

         // create an ObjectInputStream for the file we created before
         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

         // read and print a float
         System.out.println("" + ois.readFloat());

         // read and print a float
         System.out.println("" + ois.readFloat());

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

1.198743
5973.9834

Example - Writing and Reading a Single float Value

The following example shows the usage of Java ObjectInputStream readFloat() method. This example writes a single float value to a file and reads it back using readFloat().

ObjectInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectInputStreamDemo {
   public static void main(String[] args) {
      try {
         // Writing a float value to a file
         FileOutputStream fos = new FileOutputStream("float_data.dat");
         ObjectOutputStream oos = new ObjectOutputStream(fos);
         oos.writeFloat(12.34f); // Writing float value
         oos.close();

         // Reading float value using ObjectInputStream
         FileInputStream fis = new FileInputStream("float_data.dat");
         ObjectInputStream ois = new ObjectInputStream(fis);

         float value = ois.readFloat(); // Read a float value
         System.out.println("Read Float Value: " + value);

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

Output

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

Read Float Value: 12.34

Explanation

  • Writes a float value (12.34f) using writeFloat(12.34f).

  • Reads the float value using readFloat().

  • Prints the value, confirming correct storage and retrieval.

Example - Writing and Reading Multiple float Values

The following example shows the usage of Java ObjectInputStream readFloat() method. This example writes multiple float values (3.14f, 2.71f, 9.81f) and reads them sequentially.

ObjectInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectInputStreamDemo {
   public static void main(String[] args) {
      try {
         // Writing multiple float values
         FileOutputStream fos = new FileOutputStream("multiple_floats.dat");
         ObjectOutputStream oos = new ObjectOutputStream(fos);
         oos.writeFloat(3.14f);
         oos.writeFloat(2.71f);
         oos.writeFloat(9.81f);
         oos.close();

         // Reading float values using ObjectInputStream
         FileInputStream fis = new FileInputStream("multiple_floats.dat");
         ObjectInputStream ois = new ObjectInputStream(fis);

         System.out.println("First Float: " + ois.readFloat());
         System.out.println("Second Float: " + ois.readFloat());
         System.out.println("Third Float: " + ois.readFloat());

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

Output

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

First Float: 3.14
Second Float: 2.71
Third Float: 9.81

Explanation

  • Writes three float values (3.14f, 2.71f, 9.81f) to a file.

  • Reads them one by one using readFloat(), maintaining the original order.

  • Prints each float value, confirming correct serialization and deserialization.

java_io_objectinputstream.htm
Advertisements