|
Geek-Tutorials.com
|
|
The ObjectInputStream class is used for deserializing objects from input stream. The serialized data can be store on local drive and then later read by ObjectInputStream to deserialized it back to object instance. Serialization is often use as a way for saving application preferences and game state.
Classes must implement Serializable or Externalizable interfaces for ObjectOutputStream and ObjectInputStream to serialize and deserialize an instance of classes. ObjectInputStream can deserialize primitive types, classes and user defined classes. Following example show how to make object serializable by implementing Serializable interface.
package com.geek.tutorials.io.serializable;
import java.io.Serializable;
public class Class1 implements Serializable{ // Display Comment
int value;
public Class1(int value){
this.value = value;
}
public int getValue(){
return value;
}
}
Following code show how you can deserialize serializable class from ObjectInputStream and read it back to instance.
package com.geek.tutorials.io.serializable;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
public class SerializableTest1 {
public static void main(String[] args) {
Class1 class1 = new Class1(5); // Display Comment
try{
// Display Comment FileOutputStream f = new FileOutputStream("serialize1.ser");
ObjectOutputStream out = new ObjectOutputStream(f);
out.writeObject(class1); //Display Comment out.flush();
out.close();
FileInputStream f2 = new FileInputStream("serialize1.ser");
ObjectInputStream in = new ObjectInputStream(f2);
Class1 c1 =(Class1)in.readObject(); //Display Commentin.close(); System.out.println(c1.getValue()); //Display Comment
}catch(Exception e){
System.out.println(e);
}
}
}
| protected ObjectInputStream () | Constructs an instance of ObjectInputStream. |
|
Syntax
Exception
Example | |
| public ObjectInputStream (InputStream in) | Constructs an instance of ObjectInputStream that read from InputStream. |
|
Syntax
Parameters
Exception
Example | |
| public int available () | Find out number primitive data(e.g. int, boolean and etc) bytes available to read from this ObjectInputStream. |
|
Syntax
Exception
Description
Example package com.geek.tutorials.io.ObjectInputStream;
import java.io.*;
import java.util.*;
public class SkipBytesExample {
public static void main(String args[]){
try{
FileOutputStream f = new FileOutputStream("SkipBytesExample.ser");
ObjectOutput out = new ObjectOutputStream(f);
out.writeInt(10000);
out.writeInt(200);
out.writeObject(new Date());
out.close();
FileInputStream f2 = new FileInputStream("SkipBytesExample.ser");
ObjectInputStream in = new ObjectInputStream(f2);
int byteCount = in.available(); //Display Comment call available() to find out the number of primitive data bytes available in the input stream before reaching next serialized object.
System.out.println(byteCount); in.skipBytes(byteCount); //Display Comment call skipBytes() for skip bytes to the next serialized object in the input stream.
System.out.println(in.readObject());//Display Comment After skipped primitive data bytes, now it is safe to read serialized object from the input stream.
in.close();
}catch(Exception e){
System.out.println(e);
}
}
}
| |
| public void close () | Close the underlying input stream. |
|
Syntax
Exception
Description
Example | |
| public void defaultReadObject () | Read non-static and non-transient fields from the stream. |
|
Syntax
Exception
Description | |
| protected final enableResolveObject (boolean enable) | Subclass of ObjectInputStream must invoke this enableResolveObject() method with true parameter in order to successfully overrides resolveObject() method. |
|
Syntax
Exception
Example | |
| public int read () | This method read one or more bytes from underlying input stream. |
|
Syntax
Exception | |
| public boolean readBoolean () | Reads a boolean value from object input stream |
|
Syntax
Exception
Example | |
| public byte readByte () | Reads a byte value from object input stream |
|
Syntax
Exception
Example | |
| public char readChar () | Reads a char value from object input stream |
|
Syntax
Exception
Example | |
| public double readDouble () | Reads a double value from object input stream |
|
Syntax
Exception
Example | |
| public float readFloat () | Reads a float value from object input stream |
|
Syntax
Exception
Example | |
| public void readFully () | Reads bytes from input stream. |
|
Syntax
Exception
Description | |
| public int readInt () | Reads a int value from object input stream |
|
Syntax
Exception
Example | |
| public long readLong () | Reads a long value from object input stream |
|
Syntax
Exception
Example | |
| public final Object readObject () | Reads an object from object input stream |
|
Syntax IOException - If error occurs while reading data from underlying input stream. ClassNotFoundException - If the class of the object's field or the object itself being deserialized cannot be found by class loader. EOFException - If attempt to read object from input stream that reach end-of-stream. InvalidClassException - If the class being deserialized cannot not be instantiated. StreamCorruptedException - If the class descriptor data is inconsistent with target class.
Example | |
| public short readShort () | Reads a short value from object input stream |
|
Syntax
Exception
Example | |
| protected void readStreamHeader () | Reads and check the stream header from the input stream. |
|
Syntax IOException - If error occurs while reading data from underlying output stream. | |
| public int readUnsignedByte () | Reads a unsigned byte value from object input stream |
|
Syntax
Exception | |
| public int readUnsignedShort () | Reads a unsigned short value from object input stream |
|
Syntax
Exception | |
| public String readUTF () | Reads a Unicode string from object input stream and return it as a String object instance. |
|
Syntax
Exception
Example | |
| protected Class resolveClass (ObjectStreamClass desc) | Loads the class for later instantiate of object. |
|
Syntax
Exception
Example | |
| protected Object resolveObject (Object obj) | Subclass overrides this method to replaces an object from the input stream with another object. |
|
Syntax
Exception
Example | |
| public int skipBytes (int count) | skip number of bytes from the input stream |
|
Syntax
Exception
Example | |
package com.geek.tutorials.io.ObjectOutputStream;
import java.io.*;
import com.geek.tutorials.io.serializable.Class1;
public class WritePrimitiveExample {
public static void main(String arg[]){
try{
FileOutputStream f = new FileOutputStream("WritePrimiteExample.ser");
ObjectOutputStream out = new ObjectOutputStream(f);
out.writeBoolean(true); // writes a boolean
out.writeByte(2); // writes a byte
out.writeBytes("34"); // writes a bytes
out.writeChar('D'); // writes a char
out.writeChars("56"); // writes a chars
out.writeDouble(6.0); // writes a double
out.writeFloat(7.0f); // writes a float
out.writeInt(8); // writes a int
out.writeLong(999999999); // writes a long
out.writeShort(10); // writes a short
out.flush();
out.close();
FileInputStream f2 = new FileInputStream("WritePrimiteExample.ser");
ObjectInputStream in = new ObjectInputStream(f2);
boolean boolValue = in.readBoolean(); // read a boolean
byte byteValue = in.readByte(); // read a byte
int bytesValue = in.readByte(); // read first byte from bytes
int bytesValue2 = in.readByte(); // read second byte from bytes
char charValue = in.readChar(); // read a char
char chars1 = in.readChar(); // read first char from chars
char chars2 = in.readChar(); // read second char from chars
double doubleValue = in.readDouble(); // read a double
float floatValue = in.readFloat(); // read a float
int intValue = in.readInt(); // read a int
long longValue = in.readLong(); // read a long
short shortValue = in.readShort(); // read a short
in.close();
}catch(Exception e){
System.out.println(e);
}
}
}