Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java - ObjectInputStream readDouble() method



Description

The Java ObjectInputStream readDouble() method reads a double (64-bit floating-point number) from the input stream. It is used when a double value has been written using writeDouble(double d).

Declaration

Following is the declaration for java.io.ObjectInputStream.readDouble() method −

protected double readDouble()

Parameters

NA

Return Value

This method returns the 64 bit double read.

Exception

  • EOFException − If end of file is reached.

  • IOException − If an I/O error has occurred.

Example - Usage of ObjectInputStream readDouble() method

The following example shows the usage of Java ObjectInputStream readDouble() method.

ObjectInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectInputStreamDemo {
   public static void main(String[] args) {
      double b = 0380549705498d;
      
      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(b);
         oout.writeDouble(49753986394864d);
         oout.flush();

         // create an ObjectInputStream for the file we created before
         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

         // read and print a double
         System.out.println("" + ois.readDouble());

         // read and print a double
         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−

3.80549705498E11
4.9753986394864E13

Example - Writing and Reading a Single double Value

The following example shows the usage of Java ObjectInputStream readDouble() method. This example writes a single double value to a file and reads it back using readDouble().

ObjectInputStreamDemo.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 ObjectInputStreamDemo {
   public static void main(String[] args) {
      try {
         // Writing a double value to a file
         FileOutputStream fos = new FileOutputStream("double_data.dat");
         ObjectOutputStream oos = new ObjectOutputStream(fos);
         oos.writeDouble(123.456); // Writing double value
         oos.close();

         // Reading double value using ObjectInputStream
         FileInputStream fis = new FileInputStream("double_data.dat");
         ObjectInputStream ois = new ObjectInputStream(fis);

         double value = ois.readDouble(); // Read a double value
         System.out.println("Read Double Value: " + value);

         ois.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Read Double Value: 123.456

Explanation

  • Writes a double value (123.456) using writeDouble(123.456).

  • Reads the double value using readDouble().

  • Prints the value, confirming correct storage and retrieval.

Example - Writing and Reading Multiple double Values

The following example shows the usage of Java ObjectInputStream readDouble() method. This example writes multiple double values (12.34, 56.78, 90.12) and reads them sequentially.

ObjectInputStreamDemo.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 ObjectInputStreamDemo {
   public static void main(String[] args) {
      try {
         // Writing multiple double values
         FileOutputStream fos = new FileOutputStream("multiple_doubles.dat");
         ObjectOutputStream oos = new ObjectOutputStream(fos);
         oos.writeDouble(12.34);
         oos.writeDouble(56.78);
         oos.writeDouble(90.12);
         oos.close();

         // Reading double values using ObjectInputStream
         FileInputStream fis = new FileInputStream("multiple_doubles.dat");
         ObjectInputStream ois = new ObjectInputStream(fis);

         System.out.println("First Double: " + ois.readDouble());
         System.out.println("Second Double: " + ois.readDouble());
         System.out.println("Third Double: " + ois.readDouble());

         ois.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

First Double: 12.34
Second Double: 56.78
Third Double: 90.12

Explanation

  • Writes three double values (12.34, 56.78, 90.12) to a file.

  • Reads them one by one using readDouble(), maintaining the original order.

  • Prints each double value, confirming correct serialization and deserialization.

java_io_objectinputstream.htm
Advertisements