Java - ObjectInputStream readInt() method



Description

The Java ObjectInputStream readInt() method reads a single int (32-bit integer) from the input stream. It is used when an integer value has been written using writeInt(int i).

Declaration

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

public int readInt()

Parameters

NA

Return Value

This method returns the read 32 bit int.

Exception

  • EOFException − If end of file is reached.

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

Example - Usage of ObjectInputStream readInt() method

The following example shows the usage of Java ObjectInputStream readInt() 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) {
      int i = 319874;
      
      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.writeInt(i);
         oout.writeInt(1653984);
         oout.flush();

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

         // read and print an int
         System.out.println("" + ois.readInt());

         // read and print an int
         System.out.println("" + ois.readInt());
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

319874
1653984

Example - Writing and Reading a Single Integer

The following example shows the usage of Java ObjectInputStream readInt() method. This example writes a single integer value to a file and reads it back using readInt().

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) {
      try {
         // Writing an integer to a file
         FileOutputStream fos = new FileOutputStream("int_data.dat");
         ObjectOutputStream oos = new ObjectOutputStream(fos);
         oos.writeInt(12345); // Writing integer 12345
         oos.close();

         // Reading integer value using ObjectInputStream
         FileInputStream fis = new FileInputStream("int_data.dat");
         ObjectInputStream ois = new ObjectInputStream(fis);

         int value = ois.readInt(); // Read an integer
         System.out.println("Read Integer Value: " + value);

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

Output

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

Read Integer Value: 12345

Explanation

  • Writes the integer 12345 using writeInt(12345).

  • Reads the integer using readInt().

  • Prints the value, confirming correct serialization and deserialization.

Example - Writing and Reading Multiple Integers

The following example shows the usage of Java ObjectInputStream readInt() method. This example writes multiple int values (100, 200, 300) and reads them sequentially.

ObjectInputStreamDemo.java

package com.tutorialspoint;

import java.io.*;

public class ObjectInputStreamDemo {
   public static void main(String[] args) {
      try {
         // Writing multiple integer values
         FileOutputStream fos = new FileOutputStream("multiple_ints.dat");
         ObjectOutputStream oos = new ObjectOutputStream(fos);
         oos.writeInt(100);
         oos.writeInt(200);
         oos.writeInt(300);
         oos.close();

         // Reading integer values using ObjectInputStream
         FileInputStream fis = new FileInputStream("multiple_ints.dat");
         ObjectInputStream ois = new ObjectInputStream(fis);

         System.out.println("First Integer: " + ois.readInt());
         System.out.println("Second Integer: " + ois.readInt());
         System.out.println("Third Integer: " + ois.readInt());

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

Output

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

First Integer: 100
Second Integer: 200
Third Integer: 300

Explanation

  • Writes three integer values (100, 200, 300) to a file.

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

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

java_io_objectinputstream.htm
Advertisements