
- 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 - RandomAccessFile writeDouble(double v) method
Description
The Java RandomAccessFile writeDouble(double v) method converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the file as an eight-byte quantity, high byte first. The write starts at the current position of the file pointer.
Declaration
Following is the declaration for java.io.RandomAccessFile.writeDouble(double v) method.
public final void writeDouble(double v)
Parameters
v − a double value to be written.
Return Value
This method does not return a value.
Exception
IOException − If an I/O error occurs.
Example - Usage of RandomAccessFile writeDouble(double v) method
The following example shows the usage of RandomAccessFile writeDouble(double v) method.
RandomAccessFileDemo.java
package com.tutorialspoint; import java.io.RandomAccessFile; import java.io.IOException; public class RandomAccessFileDemo { public static void main(String[] args) { try { double d = 1847.4986; // create a new RandomAccessFile with filename test RandomAccessFile raf = new RandomAccessFile("test.txt", "rw"); // write a double in the file raf.writeDouble(d); // set the file pointer at 0 position raf.seek(0); // read double System.out.println(raf.readDouble()); // set the file pointer at 0 position raf.seek(0); // write a double at the start raf.writeDouble(473.5645); // set the file pointer at 0 position raf.seek(0); // read double System.out.println(raf.readDouble()); } catch (IOException ex) { ex.printStackTrace(); } } }
Output
Assuming we have a text file test.txt in current directory which has the following content. This file will be used as an input for our example program −
ABCDE
Let us compile and run the above program, this will produce the following result −
1847.4986 473.5645
Example - Write a Single Double to a File
The following example shows the usage of RandomAccessFile writeDouble(double v) method.
RandomAccessFileDemo.java
package com.tutorialspoint; import java.io.RandomAccessFile; import java.io.IOException; public class RandomAccessFileDemo { public static void main(String[] args) { try (RandomAccessFile raf = new RandomAccessFile("double1.dat", "rw")) { raf.writeDouble(123.456); System.out.println("Double written successfully."); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Double written successfully.
Explanation
123.456 is written as an 8-byte double.
File double1.dat will contain 8 bytes representing the binary form of 123.456.
You can later read it back using readDouble().
Example - Write Multiple Doubles and Read Them Back
The following example shows the usage of RandomAccessFile writeDouble(double v) method.
RandomAccessFileDemo.java
package com.tutorialspoint; import java.io.RandomAccessFile; import java.io.IOException; public class RandomAccessFileDemo { public static void main(String[] args) { try (RandomAccessFile raf = new RandomAccessFile("double2.dat", "rw")) { // Write multiple doubles raf.writeDouble(3.14159); raf.writeDouble(2.71828); raf.writeDouble(1.61803); // Move pointer to beginning raf.seek(0); // Read back values double d1 = raf.readDouble(); double d2 = raf.readDouble(); double d3 = raf.readDouble(); System.out.printf("Read values: %.5f, %.5f, %.5f%n", d1, d2, d3); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Read values: 3.14159, 2.71828, 1.61803
Explanation
Writes three double values: 3.14159, 2.71828 and 1.61803.
Each takes 8 bytes → total file size = 24 bytes.
seek(0) resets the file pointer before reading.
Values are read back exactly as written.