Saturday, February 21, 2026
HomeLanguagesJavaDataInputStream readDouble() method in Java with Examples

DataInputStream readDouble() method in Java with Examples

The readDouble() method of DataInputStream class in Java is used to read eight input bytes and returns a double value. This method reads the next eight bytes from the input stream and interprets it into double type and returns.

Syntax:

public final double readDouble()
                  throws IOException

Specified By: This method is specified by readDouble() method of DataInput interface.

Parameters: This method does not accept any parameter.

Return value: This method returns the double value interpreted by the next eight bytes of the input stream.

Exceptions:

  • EOFException – It throws EOFException if the input stream is ended before eight bytes can be read.
  • IOException – This method throws IOException if the stream is closed or some other I/O error occurs.

Below programs illustrate readDouble() method in DataInputStream class in IO package:

Program 1: Assume the existence of file “demo.txt”.




// Java program to illustrate
// DataInputStream readDouble() method
import java.io.*;
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Create double array
        double[] buf = { 10, 20, 30, 40, 50 };
  
        // Create file output stream
        FileOutputStream outputStream
            = new FileOutputStream("c:\\demo.txt");
  
        // Create data output stream
        DataOutputStream dataOutputStr
            = new DataOutputStream(outputStream);
  
        for (double b : buf) {
            // Write double value to
            // the dataOutputStream
            dataOutputStr.writeDouble(b);
        }
  
        dataOutputStr.flush();
  
        // Create file input stream
        FileInputStream inputStream
            = new FileInputStream("c:\\demo.txt");
  
        // Create data input stream
        DataInputStream dataInputStr
            = new DataInputStream(inputStream);
  
        while (dataInputStr.available() > 0) {
            // Print double values
            System.out.println(
                dataInputStr.readDouble());
        }
    }
}


Output:

Program 2: Assume the existence of file “demo.txt”.




// Java program to illustrate
// DataInputStream readDouble() method
import java.io.*;
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Create double array
        double[] buf = { 10.9, 20.8,
                         30.88, 40.76,
                         50.678 };
  
        // Create file output stream
        FileOutputStream outputStream
            = new FileOutputStream("c:\\demo.txt");
  
        // Create data output stream
        DataOutputStream dataOutputStr
            = new DataOutputStream(outputStream);
  
        for (double b : buf) {
            // Write double value to
            // the dataOutputStream
            dataOutputStr.writeDouble(b);
        }
  
        dataOutputStr.flush();
  
        // Create file input stream
        FileInputStream inputStream
            = new FileInputStream("c:\\demo.txt");
  
        // Create data input stream
        DataInputStream dataInputStr
            = new DataInputStream(inputStream);
  
        while (dataInputStr.available() > 0) {
            // Print double values
            System.out.println(
                dataInputStr.readDouble());
        }
    }
}


Output:

References:
https://docs.oracle.com/javase/10/docs/api/java/io/DataInputStream.html#readDouble()

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS