Write float to a file using DataOutputStream
- /*
- Write float to a file using DataOutputStream
- This Java example shows how to write a Java float primitive value to a file using
- writeFloat method of Java DataOutputStream class.
- */
- import java.io.DataOutputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class WriteFloatToFile {
- public static void main(String[] args) {
- String strFilePath = "C://FileIO//WriteFloat.txt";
- try
- {
- //create FileOutputStream object
- FileOutputStream fos = new FileOutputStream(strFilePath);
- /*
- * To create DataOutputStream object from FileOutputStream use,
- * DataOutputStream(OutputStream os) constructor.
- *
- */
- DataOutputStream dos = new DataOutputStream(fos);
- float f = 5.314f;
- /*
- * To write a float value to a file, use
- * void writeFloat(float f) method of Java DataOutputStream class.
- *
- * This method writes specified float to output stream as 4 bytes value.
- * Please note that the float value is first converted to int using
- * Float.floatToIntBits method and then int is written to underlying output
- * stream.
- */
- dos.writeFloat(f);
- /*
- * To close DataOutputStream use,
- * void close() method.
- *
- */
- dos.close();
- }
- catch (IOException e)
- {
- System.out.println("IOException : " + e);
- }
- }
- }



