This post originated from an RSS feed registered with Java Buzz
by instanceof java.
Original Post: ObjectInputStream and ObjectOutputStream
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
ObjectInputStream and ObjectOutputStream classes are used to store objects state permanently in files or to send to remote computer via network.
ObjectOutputStream class is subclass of ObjectOutput interface. It implements below method to write object to underline output stream..
public void writeObject(Object ob)throws IOException
ObjectInputStream class is a subclass of ObjectInput interface. It implements below method to read object to underline output stream.
public Object readObject() throws IOException
Rule:
To write or send object to external world it must be java.io.Serializable interface type.
It means this objects class must be a sub class of java.io.Serializable interface, else write() method throws java.io.NotSerializableException.
Constructors to create above two classes objects:
Like DataInputStream and DataOutputStream , ObjectInputStream and ObjectOutputStream are also can not connect to source and destination directly. So their constructors take other input and output stream class objects.
public ObjectInputStream(InputStream in)
public ObjectOutputStream(OutputStream out)
package instanceofjava;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class SerDemo implements Serializable {
String name;
String phonum;
String address;
int pin;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhonum() {
return phonum;
}
public void setPhonum(String phonum) {
this.phonum = phonum;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getPin() {
return pin;
}
public void setPin(int pin) {
this.pin = pin;
}
public static void main(String[] args)throws Exception{
SerDemo empobj= new SerDemo();
empobj.setName("sai");
empobj.setAddress("xxx, xxxstreet ");
empobj.setPhonum("040-9999999");
empobj.setPin(500072);
ObjectOutputStream oos= new ObjectOutputStream(new
FileOutputStream("E:\\employee.ser"));
oos.writeObject(empobj);
}
}
package instanceofjava;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
public class DeserDemo {
public static void main(String[] args)throws Exception{
ObjectInputStream ois= new ObjectInputStream(new FileInputStream("E:\\employee.ser"));
SerDemo obj= (SerDemo)ois.readObject();
System.out.println(obj.getAddress()):
}
}
Output:
sai
xxx, xxxstreet
500072
040-9999999
This is the actual functionality of ObjectOutputStream and ObjectInputStream: Serialization and deserialization
Serialization:
Serialization is the process of converting objects into stream of bytes and sending them to underlying output stream.
Using serialization we can store object state permanently in a destination , for example on remote computer.
Serialization operation performed by the writeObject() method of ObjectOutputStream.
Deserialization:
Deserialization is the process of converting stream of bytes into original object.
Deserialization operation is performed by readObject() of ObjectInputStream.
Rule:
Only java.io.serializable type objects are serialized. Serializable is a marker interface, it does not have any methods.
It provides special permission or identity to JVM to serialize Object.
If the given object is not of type serializable interface then writeObject() will throw an unchecked exception called java.io.NotSerializableException.