Home » Java programming language

Java DataInputStream readDouble() Method with Example

DataInputStream Class readDouble() method: Here, we are going to learn about the readDouble() method of DataInputStream Class with its syntax and example.
Submitted by Preeti Jain, on March 30, 2020

DataInputStream Class readDouble() method

  • readDouble() method is available in java.io package.
  • readDouble() method is used to read 8 bytes (i.e. 64 bit) of double value and returns a double value read.
  • readDouble() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • readDouble() method may throw an exception at the time of reading double
    • IOException: This exception may throw when this stream is not opened.
    • EndOfFileException: This exception may throw when this stream has reached its endpoint.

Syntax:

    public final double readDouble();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is double, it returns 8 bytes of data input, manipulated as double.

Example:

// Java program to demonstrate the example 
// of double readDouble() method of 
// DataInputStream

import java.io.*;

public class ReadDoubleOfDIS {
 public static void main(String[] args) throws IOException {
  InputStream is_stm = null;
  DataInputStream dis_stm = null;
  FileOutputStream fos_stm = null;
  DataOutputStream dos_stm = null;
  double[] d_arr = {
   97.0,
   98.0,
   99.0,
   100.0,
   101.0
  };

  try {
   // Instantiate FileInputStream, 
   // DataInputStream, FileOutputStream
   // and DataOutputStream 

   fos_stm = new FileOutputStream("C:\\Users\\Preeti Jain\\Desktop\\programs\\includehelp.txt");
   dos_stm = new DataOutputStream(fos_stm);

   // Loop to write each double till end
   for (double val: d_arr) {

    // By using writeDouble() method isto
    // write a double to the
    // DataOutputStream dos_stm

    dos_stm.writeDouble(val);
   }


   is_stm = new FileInputStream("C:\\Users\\Preeti Jain\\Desktop\\programs\\includehelp.txt");
   dis_stm = new DataInputStream(is_stm);

   // Loop To Read Available Data till end
   while (dis_stm.available() > 0) {

    // By using readDouble() method isto read 
    // double at a time

    double d = dis_stm.readDouble();
    System.out.println("dis_stm.readDouble(): " + d);
   }

  } catch (Exception ex) {
   System.out.println(ex.toString());
  } finally {
   // To free system resources linked
   // with these streams
   if (is_stm != null)
    is_stm.close();

   if (dis_stm != null)
    dis_stm.close();

   if (dos_stm != null)
    dos_stm.close();

   if (fos_stm != null)
    fos_stm.close();
  }
 }
}

Output

dis_stm.readDouble(): 97.0
dis_stm.readDouble(): 98.0
dis_stm.readDouble(): 99.0
dis_stm.readDouble(): 100.0
dis_stm.readDouble(): 101.0






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.