Java IO Tutorial - Java DataInputStream.readFloat()








Syntax

DataInputStream.readFloat() has the following syntax.

public final float readFloat()   throws IOException

Example

In the following code shows how to use DataInputStream.readFloat() method.

//  w  w  w  . j  ava2s. co  m
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

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

    float[] fbuf = { 123.345f, 234.567f, 123.123f, 123.123f, 123.123f, 123.345f };

    FileOutputStream fos = new FileOutputStream("c:\\test.txt");
    DataOutputStream dos = new DataOutputStream(fos);

    for (float f : fbuf) {
      dos.writeFloat(f);
    }

    dos.flush();

    InputStream is = new FileInputStream("c:\\test.txt");

    DataInputStream dis = new DataInputStream(is);

    while (dis.available() > 0) {
      float c = dis.readFloat();
      System.out.print(c + " ");
    }
  }
}