• 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.

  1. 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.
  1. 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.
  1. public ObjectInputStream(InputStream in)
  2. public ObjectOutputStream(OutputStream out)

  1. package instanceofjava;
  2.  
  3. import java.io.FileOutputStream;
  4. import java.io.ObjectOutputStream;
  5. import java.io.Serializable;
  6.  
  7. public class SerDemo implements Serializable {
  8.  
  9.     String name;
  10.     String phonum;
  11.     String address;
  12.     int pin;
  13.  
  14.    public String getName() {
  15.        return name;
  16.     }
  17.  
  18.     public void setName(String name) {
  19.         this.name = name;
  20.     }
  21.  
  22.     public String getPhonum() {
  23.         return phonum;
  24.    }
  25.  
  26.     public void setPhonum(String phonum) {
  27.         this.phonum = phonum;
  28.     }
  29.  
  30.    public String getAddress() {
  31.         return address;
  32.     }
  33.  
  34.     public void setAddress(String address) {
  35.         this.address = address;
  36.     }
  37.  
  38.    public int getPin() {
  39.         return pin;
  40.     }

  41.   public void setPin(int pin) {
  42.         this.pin = pin;
  43.     }
  44.  
  45. public static void main(String[] args)throws Exception{
  46.  
  47.     SerDemo empobj= new SerDemo();
  48.     empobj.setName("sai");
  49.     empobj.setAddress("xxx, xxxstreet ");
  50.     empobj.setPhonum("040-9999999");
  51.     empobj.setPin(500072);
  52.  
  53.     ObjectOutputStream oos= new ObjectOutputStream(new
  54.     FileOutputStream("E:\\employee.ser"));
  55.  
  56.     oos.writeObject(empobj);
  57.  
  58. }
  59. }




  1. package instanceofjava;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.ObjectInputStream;
  5.  
  6. public class DeserDemo {
  7.  
  8. public static void main(String[] args)throws Exception{   
  9.  
  10.    ObjectInputStream ois= new ObjectInputStream(new FileInputStream("E:\\employee.ser"));
  11.       SerDemo obj= (SerDemo)ois.readObject();
  12.       System.out.println(obj.getAddress()):
  13.  
  14.     }
  15. }

Output:

  1. sai
  2. xxx, xxxstreet 
  3. 500072
  4. 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.

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu