Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

DataInputStream readFloat() method in Java with Examples

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The readFloat() method of DataInputStream class in Java is used to read four input bytes and returns a float value. This method reads the next four bytes from the input stream and interprets it into float type and returns.

Syntax:

public final float readFloat()
                  throws IOException

Specified By: This method is specified by readFloat() method of DataInput interface.

Parameters: This method does not accept any parameter.

Return value: This method returns the float value interpreted by the next four bytes of the input stream.

Exceptions:

  • EOFException – It throws EOFException if the input stream is ended before four bytes can be read.
  • IOException – This method throws IOException if the stream is closed or some other I/O error occurs.

Below programs illustrate readFloat() method in DataInputStream class in IO package:

Program 1: Assume the existence of file “demo.txt”.




// Java program to illustrate
// DataInputStream readFloat() method
import java.io.*;
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Create float array
        float[] buf = { 10, 20, 30, 40, 50 };
  
        // Create file output stream
        FileOutputStream outputStream
            = new FileOutputStream("c:\\demo.txt");
  
        // Create data output stream
        DataOutputStream dataOutputStr
            = new DataOutputStream(outputStream);
  
        for (float b : buf) {
            // Write float value to
            // the dataOutputStream
            dataOutputStr.writeFloat(b);
        }
  
        dataOutputStr.flush();
  
        // Create file input stream
        FileInputStream inputStream
            = new FileInputStream("c:\\demo.txt");
  
        // Create data input stream
        DataInputStream dataInputStr
            = new DataInputStream(inputStream);
  
        while (dataInputStr.available() > 0) {
            // Print float values
            System.out.println(
                dataInputStr.readFloat());
        }
    }
}


Output:

Program 2: Assume the existence of file “demo.txt”.




// Java program to illustrate
// DataInputStream readFloat() method
import java.io.*;
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Create float array
        float[] buf = { 10.9f, 20.8f,
                        30.88f, 40.76f,
                        50.678f };
  
        // Create file output stream
        FileOutputStream outputStream
            = new FileOutputStream("c:\\demo.txt");
  
        // Create data output stream
        DataOutputStream dataOutputStr
            = new DataOutputStream(outputStream);
  
        for (float b : buf) {
            // Write float value to
            // the dataOutputStream
            dataOutputStr.writeFloat(b);
        }
  
        dataOutputStr.flush();
  
        // Create file input stream
        FileInputStream inputStream
            = new FileInputStream("c:\\demo.txt");
  
        // Create data input stream
        DataInputStream dataInputStr
            = new DataInputStream(inputStream);
  
        while (dataInputStr.available() > 0) {
            // Print float values
            System.out.println(
                dataInputStr.readFloat());
        }
    }
}


Output:

References:
https://docs.oracle.com/javase/10/docs/api/java/io/DataInputStream.html#readFloat()


My Personal Notes arrow_drop_up
Last Updated : 05 Jun, 2020
Like Article
Save Article
Similar Reads
Related Tutorials