ObjectInputStream class

                                      
    java.lang.Object                                 
     |                                
     |--java.io.InputStream                             
         |                            
         |--java.io.ObjectInputStream                         
                                      

An ObjectInputStream deserializes primitive data and objects previously written using an ObjectOutputStream.

ConstructorSummary
ObjectInputStream(InputStream in)Creates an ObjectInputStream that reads from the specified InputStream.

ReturnMethodSummary
intavailable()Returns the number of bytes that can be read without blocking.
voidclose()Closes the input stream.
voiddefaultReadObject()Read the non-static and non-transient fields of the current class from this stream.
intread()Reads a byte of data.
intread(byte[] buf, int off, int len)Reads into an array of bytes.
booleanreadBoolean()Reads in a boolean.
bytereadByte()Reads an 8 bit byte.
charreadChar()Reads a 16 bit char.
doublereadDouble()Reads a 64 bit double.
floatreadFloat()Reads a 32 bit float.
voidreadFully(byte[] buf)Reads bytes, blocking until all bytes are read.
voidreadFully(byte[] buf, int off, int len)Reads bytes, blocking until all bytes are read.
intreadInt()Reads a 32 bit int.
StringreadLine()Deprecated. This method does not properly convert bytes to characters. see DataInputStream for the details and alternatives.
longreadLong()Reads a 64 bit long.
ObjectreadObject()Read an object from the ObjectInputStream.
shortreadShort()Reads a 16 bit short.
ObjectreadUnshared()Reads an "unshared" object from the ObjectInputStream.
intreadUnsignedByte()Reads an unsigned 8 bit byte.
intreadUnsignedShort()Reads an unsigned 16 bit short.
StringreadUTF()Reads a String in modified UTF-8 format.
voidregisterValidation(ObjectInputValidation obj, int prio)Register an object to be validated before the graph is returned.
intskipBytes(int len)Skips bytes.
Revised from Open JDK source code

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;

public class Main {
  static final String dataFile = "data.dat";

  public static void main(String[] args) throws IOException, ClassNotFoundException {

    ObjectOutputStream out = null;
    try {
      out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));

      out.writeObject(new BigInteger("123"));
      out.writeInt(123);
      out.writeUTF("a String");
    } finally {
      out.close();
    }

    ObjectInputStream in = null;
    try {
      in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(dataFile)));
      try {
        while (true) {
          BigInteger price = (BigInteger) in.readObject();
          int unit = in.readInt();
          String desc = in.readUTF();
          System.out.println(price);
          System.out.println(unit);
          System.out.println(desc);
        }
      } catch (EOFException e) {
      }

    } finally {
      in.close();
    }
  }
}

The output:


123
123
a String
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.