
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
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.