Java - DataOutputStream writeDouble(double value) method



Description

The Java DataOutputStream writeDouble(double value) method method converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the underlying output stream as an 8-byte quantity, high byte first. If no exception is thrown, the counter written is incremented by 8.

Declaration

Following is the declaration for java.io.DataOutputStream.writeDouble(double value) method −

public final void writeDouble(double value)

Parameters

v − a double value to be written to the output stream.

Return Value

This method does not return any value.

Exception

IOException − If an I/O error occurs.

Example - Usage of DataOutputStream writeDouble(double value) method

The following example shows the usage of Java DataOutputStream writeDouble(double value) method. We've created InputStream, DataInputStream, FileOutputStream and DataOutputStream reference. A double[] buf is initialized with some double values. A FileOutputStream object is created. Then DataOutputStream is initialized with FileOutputStream object created before. Then double array is iterated to write double values using writeDouble() method to the dataoutputstream.

Once double array is fully written into the stream, we've flushed the stream to store the values in the file. Now using FileInputStream and DataInputStream, we're reading the file written earlier. Now we're checking if DataInputStream object has data using available() method. Then using readDouble() method, we're reading every value as double. Finally we're closing all the streams.

DataOutputStreamDemo.java

package com.tutorialspoint;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class DataOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      InputStream is = null;
      DataInputStream dis = null;
      FileOutputStream fos = null;
      DataOutputStream dos = null;
      double[] dbuf = {65.56,66.89,67.98,68.82,69.55,70.37};
      
      try {
         // create file output stream
         fos = new FileOutputStream("test.txt");
         
         // create data output stream
         dos = new DataOutputStream(fos);
         
         // for each byte in the buffer
         for (double d:dbuf) {
         
            // write double to the data output stream
            dos.writeDouble(d);         
         }
         
         // force bytes to the underlying stream
         dos.flush();
         
         // create file input stream
         is = new FileInputStream("test.txt");
         
         // create new data input stream
         dis = new DataInputStream(is);
         
         // read till end of the stream
         while(dis.available()>0) {
         
            // read double
            double c = dis.readDouble();
            
            // print
            System.out.print(c + " ");
         }
         
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases all system resources from the streams
         if(is!=null)
            is.close();
         if(dos!=null)
            is.close();
         if(dis!=null)
            dis.close();
         if(fos!=null)
            fos.close();
      }
   }
}

Output

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

65.56 66.89 67.98 68.82 69.55 70.37 

Example - Usage of DataOutputStream writeDouble(double value) method

The following example shows the usage of Java DataOutputStream writeDouble(double value) method. We've created InputStream, DataInputStream, FileOutputStream and DataOutputStream reference. A double[] buf is initialized with some double values. A FileOutputStream object is created. Then DataOutputStream is initialized with FileOutputStream object created before. Then double array is iterated to write double values using writeDouble() method to the dataoutputstream. As a special case, we're closing the stream before writing any value to check if it supports writing values after closing it.

Then double array is iterated to write double values using writeDouble() method to the dataoutputstream. Once double arrays is fully written into the stream, we've flush the stream to store the values in the file. Now using FileInputStream and DataInputStream, we're reading the file written earlier. Now we're checking if DataInputStream object has data using available() method. Then using readDouble() method, we're reading every value as double. Finally we're closing all the streams.

DataOutputStreamDemo.java

package com.tutorialspoint;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class DataOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      InputStream is = null;
      DataInputStream dis = null;
      FileOutputStream fos = null;
      DataOutputStream dos = null;
      double[] dbuf = {65.56,66.89,67.98,68.82,69.55,70.37};
      
      try {
         // create file output stream
         fos = new FileOutputStream("test.txt");
         
         // create data output stream
         dos = new DataOutputStream(fos);

         // close the stream
         dos.close(); 
		 
         // for each byte in the buffer
         for (double d:dbuf) {
         
            // write double to the data output stream
            dos.writeDouble(d);         
         }
         
         // force bytes to the underlying stream
         dos.flush();
         
         // create file input stream
         is = new FileInputStream("test.txt");
         
         // create new data input stream
         dis = new DataInputStream(is);
         
         // read till end of the stream
         while(dis.available()>0) {
         
            // read double
            double c = dis.readDouble();
            
            // print
            System.out.print(c + " ");
         }
         
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases all system resources from the streams
         if(is!=null)
            is.close();
         if(dos!=null)
            is.close();
         if(dis!=null)
            dis.close();
         if(fos!=null)
            fos.close();
      }
   }
}

Output

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

java.io.IOException: Stream Closed
	at java.base/java.io.FileOutputStream.writeBytes(Native Method)
	at java.base/java.io.FileOutputStream.write(FileOutputStream.java:367)
	at java.base/java.io.DataOutputStream.writeDouble(DataOutputStream.java:259)
	at com.tutorialspoint.DataOutputStreamDemo.main(DataOutputStreamDemo.java:32)
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.io.InputStream.close()" because "is" is null
	at com.tutorialspoint.DataOutputStreamDemo.main(DataOutputStreamDemo.java:62)

As underlying stream FileOutputStream is not supporting write to stream after closing it, we get exception in program execution.

Example - Writing and Reading a Double Using writeDouble(double value)

The following example shows the usage of Java DataOutputStream writeDouble(double value) method.

DataOutputStreamDemo.java

package com.tutorialspoint;

import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class DataOutputStreamDemo {
   public static void main(String[] args) {
      try {
         // Create a DataOutputStream to write to a file
         FileOutputStream fileOutput = new FileOutputStream("output.dat");
         DataOutputStream dataOutput = new DataOutputStream(fileOutput);

         // Write double values to the file
         dataOutput.writeDouble(3.14159);  // Pi approximation
         dataOutput.writeDouble(9.81);     // Gravity acceleration (m/s)
         dataOutput.writeDouble(-1234.567);// Negative double value

         // Close the output stream
         dataOutput.close();
         System.out.println("Double values successfully written to output.dat");

         // Read the doubles back
         FileInputStream fileInput = new FileInputStream("output.dat");
         DataInputStream dataInput = new DataInputStream(fileInput);

         // Read and print the double values
         System.out.println("First double: " + dataInput.readDouble());  // 3.14159
         System.out.println("Second double: " + dataInput.readDouble()); // 9.81
         System.out.println("Third double: " + dataInput.readDouble());  // -1234.567

         // Close the input stream
         dataInput.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Double values successfully written to output.dat
First double: 3.14159
Second double: 9.81
Third double: -1234.567

Explanation

Writing Double values

  • writeDouble(3.14159) − Writes 3.14159 as 8 bytes.

  • writeDouble(9.81) − Writes 9.81 as 8 bytes.

  • writeDouble(-1234.567) − Writes -1234.567 as 8 bytes.

Reading Double values

  • readDouble() reads back 8 bytes at a time and reconstructs them into double values.

java_io_dataoutputstream.htm
Advertisements