Signup/Sign In

Java DataInputStream readDouble() Method

In this tutorial, we will learn about the readDouble() method of DataInputStream class in Java. This method is used to read 8 input bytes (i.e. 64 bits) and returns a double value. It is a non-static method, available in the java.io package.

Syntax

This is the syntax declaration of this method. It does not accept any parameter and returns the double value interpreted by the next eight bytes of the current input stream.

public final double readDouble() throws IOException

Example: Read Data using DataInputStram in Java

In this example, we are reading data from the file in a Double type and its size will be 64 bits, here important observation to note, even the provided data is of the integer type it will perform the typecast and will convert it to the double type.

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class StudyTonight 
{
	public static void main(String[] args) throws IOException 
	{ 
		FileInputStream inputStream  = new FileInputStream("E:\\studytonight\\output.txt"); 

		DataInputStream dataInputStream = new DataInputStream(inputStream); 

		while(dataInputStream.available()>0)
		{	           
            System.out.print(" "+dataInputStream.readDouble());  
        }  
	}  
}


17.0
15.0
26.0
31.0
40.0

output.txt

17
15
26
31
40

Example: Read Data using DataInputStram in Java

When we try to read the data from the file using readDouble() and the data is not available or the pointer of the stream points to the end of the file then it will throw an exception EOFException that is End Of File Exception.

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class StudyTonight 
{
	public static void main(String[] args) throws IOException 
	{ 
		FileInputStream inputStream  = new FileInputStream("E:\\studytonight\\file.txt"); 
		DataInputStream dataInputStream = new DataInputStream(inputStream); 
		while(dataInputStream.available()>0)
		{	           
            System.out.print(" "+dataInputStream.readDouble());  
        }  
	}  
}


Exception in thread "main" java.io.EOFException
at java.base/java.io.DataInputStream.readFully(DataInputStream.java:201)
at java.base/java.io.DataInputStream.readLong(DataInputStream.java:420)
at java.base/java.io.DataInputStream.readDouble(DataInputStream.java:472)
at studytonight.StudyTonight.main(StudyTonight.java:16)

Conclusion

In this tutorial, we learned about the readDouble() method of DataInputStream class in Java, which reads the 8 input bytes of double value and returns the double value read. This method must be accessed using a class object only, trying to access it with the class name will result in an error.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.