
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - ObjectOutputStream writeFloat(float val) method
Description
The Java ObjectOutputStream writeFloat(float val) method writes a 32 bit float.
It writes the 32-bit binary representation of the float value to the output stream.
This is not object serialization - it's raw binary writing.
It's part of the DataOutput interface, which ObjectOutputStream implements.
Declaration
Following is the declaration for java.io.ObjectOutputStream.writeFloat(float val) method.
public void writeFloat(float val)
Parameters
val − The float value to be written.
Return Value
This method does not return a value.
Exception
IOException − If I/O errors occur while writing to the underlying stream.
Example - Usage of ObjectOutputStream writeFloat(float val) method
The following example shows the usage of ObjectOutputStream writeFloat(float val) method.
ObjectOutputStreamDemo.java
package com.tutorialspoint; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class ObjectOutputStreamDemo { public static void main(String[] args) { float f = 1.59875f; 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(49.7634764f); // close the stream oout.close(); // create an ObjectInputStream for the file we created before ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt")); // read and print what we wrote before System.out.println("" + ois.readFloat()); 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.59875 49.763477
Example - Write a single float value and read it back
The following example shows the usage of ObjectOutputStream writeFloat(float val) method. We're writing a temperature value 36.5f using writeFloat() and read it back.
ObjectOutputStreamDemo.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 ObjectOutputStreamDemo { public static void main(String[] args) { String filename = "float1.bin"; // Write a float value try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) { float temp = 36.5f; oos.writeFloat(temp); System.out.println("Wrote float: " + temp); } catch (IOException e) { e.printStackTrace(); } // Read the float value try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) { float result = ois.readFloat(); System.out.println("Read float: " + result); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Wrote float: 36.5 Read float: 36.5
Example - Write multiple float values (e.g. measurements) in sequence
The following example shows the usage of ObjectOutputStream writeFloat(float val) method. We're writing an array of float values representing sensor readings.
ObjectOutputStreamDemo.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 ObjectOutputStreamDemo { public static void main(String[] args) { String filename = "float_readings.bin"; float[] readings = { 1.1f, 2.5f, 3.75f, 4.0f }; // Write the float values try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) { for (float f : readings) { oos.writeFloat(f); } System.out.println("Wrote " + readings.length + " float values."); } catch (IOException e) { e.printStackTrace(); } // Read them back try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) { System.out.println("Reading float values:"); for (int i = 0; i < readings.length; i++) { float f = ois.readFloat(); System.out.println(" - " + f); } } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Wrote 4 float values. Reading float values: - 1.1 - 2.5 - 3.75 - 4.0