A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/datainputstream-readlong-method-in-java-with-examples/ below:

DataInputStream readLong() method in Java with Examples

DataInputStream readLong() method in Java with Examples

Last Updated : 05 Jun, 2020

The

readLong()

method of

DataInputStream

class in Java is used to read eight input bytes and returns a long value. This method reads the next eight bytes from the input stream and interprets it into long type and returns.

Syntax:
public final long readLong()
                  throws IOException
Specified By:

This method is specified by readLong() method of

DataInput

interface.

Parameters:

This method does not accept any parameter.

Return value:

This method returns the long value interpreted by the next eight bytes of the input stream.

Exceptions:

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

Program 1:

Assume the existence of file "demo.txt".

Java
// Java program to illustrate
// DataInputStream readLong() method
import java.io.*;
public class GFG {
    public static void main(String[] args)
        throws IOException
    {

        // Create long array
        long[] buf = { 10000000000l,
                       20000000000l,
                       30000000000l };

        // Create file output stream
        FileOutputStream outputStream
            = new FileOutputStream("c:\\demo.txt");

        // Create data output stream
        DataOutputStream dataOutputStr
            = new DataOutputStream(outputStream);

        for (long b : buf) {
            // Write long value to
            // the dataOutputStream
            dataOutputStr.writeLong(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 long values
            System.out.println(
                dataInputStr.readLong());
        }
    }
}
Output: Program 2:

Assume the existence of file "demo.txt".

Java
// Java program to illustrate
// DataInputStream readLong() method
import java.io.*;
public class GFG {
    public static void main(String[] args)
        throws IOException
    {

        // Create long array
        long[] buf = { 1234567890L,
                       9876543210L,
                       12345678910L };

        // Create file output stream
        FileOutputStream outputStream
            = new FileOutputStream("c:\\demo.txt");

        // Create data output stream
        DataOutputStream dataOutputStr
            = new DataOutputStream(outputStream);

        for (long b : buf) {
            // Write long value to
            // the dataOutputStream
            dataOutputStr.writeLong(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 long values
            System.out.println(
                dataInputStr.readLong());
        }
    }
}
Output: References: https://docs.oracle.com/javase/10/docs/api/java/io/DataInputStream.html#readLong()

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4