Java - ObjectInputStream readLong() method



Description

The Java ObjectInputStream readLong() method reads a 64 bit long.

Declaration

Following is the declaration for java.io.ObjectInputStream.readLong() method −

 public long readLong() 

Parameters

NA

Return Value

This method returns the read 64 bit long.

Exception

  • EOFException − If end of file is reached.

  • IOException − If an I/O error has occurred.

Example - Usage of ObjectInputStream readLong() method

The following example shows the usage of Java ObjectInputStream readLong() method.

ObjectInputStreamDemo.java

 package com.tutorialspoint; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class ObjectInputStreamDemo { public static void main(String[] args) { long l = 397638763597645l; 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(8364876387634l); oout.flush(); // create an ObjectInputStream for the file we created before ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt")); // read and print a long System.out.println("" + ois.readLong()); // read and print a long 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−

 397638763597645 8364876387634 

Example - Reading Fields along with Missing Fields

The following example shows the usage of Java ObjectInputStream readLong() method. This example demonstrates how to write a long value to a file using ObjectOutputStream and then read it back using ObjectInputStream.

ObjectInputStreamDemo.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 ObjectInputStreamDemo { public static void main(String[] args) { String filename = "longData.bin"; // Writing a long value to a file try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) { long num = 123456789L; oos.writeLong(num); System.out.println("Long value written: " + num); } catch (IOException e) { e.printStackTrace(); } // Reading the long value from the file try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) { long readNum = ois.readLong(); System.out.println("Long value read: " + readNum); } catch (IOException e) { e.printStackTrace(); } } } 

Output

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

 Long value written: 123456789 Long value read: 123456789 

Explanation

  • The program writes a long value (123456789L) to a binary file (longData.bin) using ObjectOutputStream.

  • It then reads the same long value back using ObjectInputStream.readLong().

  • This ensures that the stored value can be retrieved correctly.

Example - Reading multiple long values from a file

The following example shows the usage of Java ObjectInputStream readLong() method. This example writes multiple long values to a file and reads them back sequentially.

ObjectInputStreamDemo.java

 package com.tutorialspoint; import java.io.EOFException; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class ObjectInputStreamDemo { public static void main(String[] args) { String filename = "multiLongData.bin"; // Writing multiple long values to a file try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) { long[] numbers = {100000L, 200000L, 300000L}; for (long num : numbers) { oos.writeLong(num); } System.out.println("Long values written."); } catch (IOException e) { e.printStackTrace(); } // Reading multiple long values from the file try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) { while (true) { try { long readNum = ois.readLong(); System.out.println("Long value read: " + readNum); } catch (EOFException e) { break; // End of file reached } } } catch (IOException e) { e.printStackTrace(); } } } 

Output

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

 Long values written. Long value read: 100000 Long value read: 200000 Long value read: 300000 

Explanation

  • Writes three long values (100000, 200000, 300000) to a file.

  • Reads them one by one using readLong(), maintaining the original order.

  • Prints each long value, confirming correct serialization and deserialization.

java_io_objectinputstream.htm
Advertisements