GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java - ObjectInputStream readDouble() method
Selected Reading
Java - ObjectInputStream readDouble() methodDescriptionThe 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). DeclarationFollowing is the declaration for java.io.ObjectInputStream.readDouble() method − protected double readDouble() ParametersNA Return ValueThis method returns the 64 bit double read. Exception
Example - Usage of ObjectInputStream readDouble() methodThe following example shows the usage of Java ObjectInputStream readDouble() method. ObjectInputStreamDemo.javapackage 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();
}
}
}
OutputLet us compile and run the above program, this will produce the following result− 3.80549705498E11 4.9753986394864E13 Example - Writing and Reading a Single double ValueThe 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.javapackage 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();
}
}
}
OutputLet us compile and run the above program, this will produce the following result− Read Double Value: 123.456 Explanation
Example - Writing and Reading Multiple double ValuesThe 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.javapackage 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();
}
}
}
OutputLet 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
java_io_objectinputstream.htm Advertisements
|
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |