Java - DataOutputStream writeLong(long v) method



Description

The Java DataOutputStream writeLong(long v) method writes a long value to the to the underlying stream as eight bytes. The counter written is incremented by 8 on successful invocation of this method.

Declaration

Following is the declaration for java.io.DataOutputStream.writeLong(long v) method −

public final void writeLong(long v)

Parameters

v − a long value to be written to the output stream.

Return Value

The method returns the value of the written field.

Exception

IOException − If an I/O error occurs.

Example - Usage of DataOutputStream writeLong(long v) method

The following example shows the usage of Java DataOutputStream writeLong(long v) method. We've created InputStream, DataInputStream, FileOutputStream and DataOutputStream reference. A long[] buf is initialized with some long values. A FileOutputStream object is created. Then DataOutputStream is initialized with FileOutputStream object created before. Then long array is iterated to write long values using writeInt() method to the dataoutputstream.

Once long array is fully written into the stream, we've flushed the stream to store the values in the file. Now using FileInputStream and DataInputStream, we're reading the file written earlier. Now we're checking if DataInputStream object has data using available() method. Then using readLong() method, we're reading every value as long. Finally we're closing all the streams.

DataOutputStreamDemo.java

package com.tutorialspoint;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class DataOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      InputStream is = null;
      DataInputStream dis = null;
      FileOutputStream fos = null;
      DataOutputStream dos = null;
      long[] dbuf = {65,66,67,68,69,70};
      
      try {
         // create file output stream
         fos = new FileOutputStream("test.txt");
         
         // create data output stream
         dos = new DataOutputStream(fos);
         
         // for each byte in the buffer
         for (long d:dbuf) {
         
            // write long to the data output stream
            dos.writeLong(d);         
         }
         
         // force bytes to the underlying stream
         dos.flush();
         
         // create file input stream
         is = new FileInputStream("test.txt");
         
         // create new data input stream
         dis = new DataInputStream(is);
         
         // read till end of the stream
         while(dis.available()>0) {
         
            // read long
            long c = dis.readLong();
            
            // print
            System.out.print(c + " ");
         }
         
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases all system resources from the streams
         if(is!=null)
            is.close();
         if(dos!=null)
            is.close();
         if(dis!=null)
            dis.close();
         if(fos!=null)
            fos.close();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

65 66 67 68 69 70

Example - Usage of DataOutputStream writeLong(long v) method

The following example shows the usage of Java DataOutputStream writeLong(long v) method. We've created InputStream, DataInputStream, FileOutputStream and DataOutputStream reference. A long[] buf is initialized with some long values. A FileOutputStream object is created. Then DataOutputStream is initialized with FileOutputStream object created before. Then long array is iterated to write long values using writeLong() method to the dataoutputstream.

As a special case, we're closing the stream before writing any value to check if it supports writing values after closing it.

Once long array is fully written into the stream, we've flushed the stream to store the values in the file. Now using FileInputStream and DataInputStream, we're reading the file written earlier. Now we're checking if DataInputStream object has data using available() method. Then using readLong() method, we're reading every value as long. Finally we're closing all the streams.

DataOutputStreamDemo.java

package com.tutorialspoint;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class DataOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      InputStream is = null;
      DataInputStream dis = null;
      FileOutputStream fos = null;
      DataOutputStream dos = null;
      long[] dbuf = {65,66,67,68,69,70};
      
      try {
         // create file output stream
         fos = new FileOutputStream("test.txt");
         
         // create data output stream
         dos = new DataOutputStream(fos);

         // close the stream
         dos.close(); 
		 
         // for each byte in the buffer
         for (long d:dbuf) {
         
            // write long to the data output stream
            dos.writeLong(d);         
         }
         
         // force bytes to the underlying stream
         dos.flush();
         
         // create file input stream
         is = new FileInputStream("test.txt");
         
         // create new data input stream
         dis = new DataInputStream(is);
         
         // read till end of the stream
         while(dis.available()>0) {
         
            // read long
            long c = dis.readLong();
            
            // print
            System.out.print(c + " ");
         }
         
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases all system resources from the streams
         if(is!=null)
            is.close();
         if(dos!=null)
            is.close();
         if(dis!=null)
            dis.close();
         if(fos!=null)
            fos.close();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

java.io.IOException: Stream Closed
	at java.base/java.io.FileOutputStream.writeBytes(Native Method)
	at java.base/java.io.FileOutputStream.write(FileOutputStream.java:367)
	at java.base/java.io.DataOutputStream.writeLong(DataOutputStream.java:221)
	at com.tutorialspoint.DataOutputStreamDemo.main(DataOutputStreamDemo.java:32)
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.io.InputStream.close()" because "is" is null
	at com.tutorialspoint.DataOutputStreamDemo.main(DataOutputStreamDemo.java:62)

As underlying stream FileOutputStream is not supporting write to stream after closing it, we get exception in program execution.

Example - Usage of DataOutputStream writeLong(long v) method

The following example shows the usage of Java DataOutputStream writeLong(long v) method.

DataOutputStreamDemo.java

package com.tutorialspoint;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataOutputStreamDemo {
   public static void main(String[] args) {
      try (FileOutputStream fos = new FileOutputStream("output.dat");
         DataOutputStream dos = new DataOutputStream(fos)) {

         long number = 1234567890123L; // Long value
         dos.writeLong(number); // Writes 8 bytes

         FileInputStream fis = new FileInputStream("output.dat");
         DataInputStream dis = new DataInputStream(fis);

         long num = dis.readLong(); // Reads 8 bytes as a long
         System.out.println("Read Long: " + num);

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Read Long: 1234567890123

Explanation

  • FileOutputStream− Creates a binary file "output.dat" for writing.

  • DataOutputStream− Wraps FileOutputStream to enable writing primitive data types.

  • writeLong(long v) Method− Converts 1234567890123L (0x0000011F71FB04CB in hexadecimal) into 8 bytes. Stores them in big-endian order−

    • Byte 1: 00

    • Byte 2: 00

    • Byte 3: 01

    • Byte 4: 1F

    • Byte 5: 71

    • Byte 6: FB

    • Byte 7: 04

    • Byte 8: CB

Binary Representation (output.dat)

The integer 1234567890123L (0x0000011F71FB04CB) is stored as−

00000000  00000000  00000001  00011111  
01110001  11111011  00000100  11001011
java_io_dataoutputstream.htm
Advertisements