Java ObjectInputStream.readInt()

Syntax

ObjectInputStream.readInt() has the following syntax.

public int readInt()  throws IOException

Example

In the following code shows how to use ObjectInputStream.readInt() method.


/*from www .j  a  va2s  .c  o m*/


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

public class Main {

  public static void main(String[] args) throws Exception {

    int i = 123456;

    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    // write something in the file
    oout.writeInt(i);
    oout.writeInt(54321);
    oout.flush();
    oout.close();
    // 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());
    ois.close();
  }
}

The code above generates the following result.