Java - ObjectOutputStream writeLong(long val) method



Description

The Java ObjectOutputStream writeLong(long val) method writes a 64 bit long.

  • It writes the 8-byte (64-bit) binary representation of a long value to the output stream.

  • Not object serialization - it's raw binary writing.

  • Use readLong() to read the value back.

Declaration

Following is the declaration for java.io.ObjectOutputStream.writeLong(long val) method.

public void writeLong(long val)

Parameters

  • val − The long value to be written.

Return Value

This method does not return a value.

Exception

  • IOException − If I/O errors occur while writing to the underlying stream.

Example - Usage of ObjectOutputStream writeLong(long val) method

The following example shows the usage of ObjectOutputStream writeLong(long val) method.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) {
      long l = 976349698745987459l;
      
      try {
         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStream oout = new ObjectOutputStream(out);

         // write something in the file
         oout.writeLong(l);
         oout.writeLong(984876837644l);

         // close the stream
         oout.close();

         // create an ObjectInputStream for the file we created before
         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

         // read and print what we wrote before
         System.out.println("" + ois.readLong());
         System.out.println("" + ois.readLong());
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

976349698745987459
984876837644

Example - Write and read a single long timestamp

The following example shows the usage of ObjectOutputStream writeLong(long val) method. We're writing a long value representing the current timestamp.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) {
      String filename = "timestamp.bin";
      long timestamp = System.currentTimeMillis();

      // Write the long value
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
         oos.writeLong(timestamp);
         System.out.println("Wrote timestamp: " + timestamp);
      } catch (IOException e) {
         e.printStackTrace();
      }

      // Read it back
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
         long readTimestamp = ois.readLong();
         System.out.println("Read timestamp: " + readTimestamp);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Wrote timestamp: 1711378249123
Read timestamp: 1711378249123

Example - Write multiple long values (e.g. account balances)

The following example shows the usage of ObjectOutputStream writeLong(long val) method. We're writing an array of long values representing large currency amounts.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.*;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) {
      String filename = "balances.bin";
      long[] balances = {100_000_000L, 250_000_000L, 500_000_000L};

      // Write all balances
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
         for (long balance : balances) {
            oos.writeLong(balance);
         }
         System.out.println("Wrote account balances.");
      } catch (IOException e) {
         e.printStackTrace();
      }

      // Read them back
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
         System.out.println("Reading balances:");
         for (int i = 0; i < balances.length; i++) {
            long bal = ois.readLong();
            System.out.println(" - " + bal);
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Wrote account balances.
Reading balances:
 - 100000000
 - 250000000
 - 500000000
java_io_objectoutputstream.htm
Advertisements