DataOutputStream writeFloat(float) in Java

writeFloat(float): This method is available in the java.io.DataOutputStream class of Java.

Syntax:

void java.io.DataOutputStream.writeFloat(float v) throws IOException

This method takes one argument. This method converts the float argument to an int using the floatToIntBits method in class Float and then writes that int value to the underlying output stream as a 4-byte quantity, high byte first.

Note: If no exception is thrown, the counter written is incremented by 4.

Parameters: One parameter is required for this method.

v: a float value to be written.

Returns: NA

Throws:

IOException - if an I/O error occurs.

Approach

Java

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataOutputStreamwriteFloat {
    public static void main(String[] args) throws IOException {

        FileOutputStream file =
new FileOutputStream("D:\\hello.txt");
        DataOutputStream data = new DataOutputStream(file);

        float v = 1001.89f;
        data.writeFloat(v);
        System.out.println("Successfully writes");
        data.close();

    }
}

Output:

Successfully writes


No comments:

Post a Comment