The Artima Developer Community
Sponsored Link

Java Buzz Forum
ObjectInputStream and ObjectOutputStream

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
ObjectInputStream and ObjectOutputStream Posted: Mar 1, 2015 9:42 AM
Reply to this message Reply

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.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

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

Read: ObjectInputStream and ObjectOutputStream

Topic: TestNG Spring Integration Example Previous Topic   Next Topic Topic: Very fast Camels and Cloud Messaging

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use