
- 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 writeDouble(double val) method
Description
The Java ObjectOutputStream writeDouble(double val) method writes a 64 bit double.
Writes the double value as 8 bytes (64 bits) to the stream in big-endian order.
It does not serialize a Double object - just writes the binary representation.
It's part of the DataOutput interface, which ObjectOutputStream implements.
Declaration
Following is the declaration for java.io.ObjectOutputStream.writeDouble(double val) method.
public void writeDouble(double val)
Parameters
val − The double 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 writeDouble(double val) method
The following example shows the usage of ObjectOutputStream writeDouble(double 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) { double d = 1.59875d; 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.writeDouble(d); oout.writeDouble(9673.49764d); // 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.readDouble()); System.out.println("" + ois.readDouble()); } catch (Exception ex) { ex.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result −
1.59875 9673.49764
Example - Write and read a single double value
The following example shows the usage of ObjectOutputStream writeDouble(double val) method. We're writing a temperature value (36.6) using writeDouble(), and reading 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 = "double1.bin"; // Write a single double value try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) { double temp = 36.6; oos.writeDouble(temp); System.out.println("Wrote double: " + temp); } catch (IOException e) { e.printStackTrace(); } // Read the value back try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) { double value = ois.readDouble(); System.out.println("Read double: " + value); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Wrote double: 36.6 Read double: 36.6
Example - Write multiple double values to simulate a dataset
The following example shows the usage of ObjectOutputStream writeDouble(double val) method. We're writing several double values representing prices, and read them 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 = "double_prices.bin"; double[] prices = {19.99, 5.75, 99.0, 149.49}; // Write all prices using writeDouble try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) { for (double price : prices) { oos.writeDouble(price); } System.out.println("Wrote " + prices.length + " double values."); } catch (IOException e) { e.printStackTrace(); } // Read them back try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) { System.out.println("Read values:"); for (int i = 0; i < prices.length; i++) { double value = ois.readDouble(); System.out.println(" - " + value); } } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Wrote 4 double values. Read values: - 19.99 - 5.75 - 99.0 - 149.49