Home » Java programming language

Java DataInputStream readLong() Method with Example

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

DataInputStream Class readLong() method

  • readLong() method is available in java.io package.
  • readLong() method is used to read 8 bytes (i.e. 64 bit) of data input and returns a long value read.
  • readLong() 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.
  • readLong() method may throw an exception at the time of reading long.
    • 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 long readLong();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is long, it returns 8 bytes of long value, manipulated as a long value.

Example:

// Java program to demonstrate the example 
// of long readLong() method of 
// DataInputStream

import java.io.*;

public class WriteLongOfDOS {
 public static void main(String[] args) throws Exception {
  InputStream is_stm = null;
  DataInputStream dis_stm = null;
  FileOutputStream fos_stm = null;
  DataOutputStream dos_stm = null;

  long[] l_arr = {
   5411489332534l,
   234773224114l
  };

  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);

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


   for (long val: l_arr) {
    // By using writeLong() method isto
    // write long value to the
    // DataOutputStream dos_stm
    dos_stm.writeLong(val);
   }

   // By using flush() method isto
   // flush the bytes to the basic 
   // output stream         
   dos_stm.flush();

   // Loop To Read Available Data till end
   while (dis_stm.available() > 0) {
    // By using readLong() method isto read 
    // long from dis_stm
    long val = dis_stm.readLong();
    System.out.println("dos_stm.writeLong(val): " + val);
   }
  } 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

dos_stm.writeLong(val): 5411489332534
dos_stm.writeLong(val): 234773224114





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.