TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Monday 13 April 2020

serialization and deserialization in java

       Serialization and Deserialization in Java

Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object.
A Java object serializable we implement the java.io.Serializable interface.
The ObjectOutputStream class contains writeObject() method for serializing an Object.

serialization and deserialization in java example


Fig: serialization and deserialization in java


public final void writeObject(Object obj)
Throws IOException

The ObjectInputStream class contains readObject() method for deserializing an object.

public final void readObject()
Throws IOException
ClassNotFoundException

Advantages of Serialization

1.To save/persist state of an object.
2. To travel an object across a network.

Only the objects of those classes can be serialized which are implementing java.io.Serializable interface.
Serializable is a marker interface (has no data member and method). It is used to “mark” java classes so that objects of these classes may get certain capability. 

Example:

// Java code for serialization and deserialization
// of a Java object
import java.io.*;
class Demo implements java.io.Serializable
{
          public int a;
          public String b;
          // Default constructor
          public Demo(int a, String b)
          {
                   this.a = a;
                   this.b = b;
          } }

class Test
{
          public static void main(String[] args)
          {
                   Demo object = new Demo(1, "today java school");
                   String filename = "file.ser";
                   // Serialization
                   try
                   {
                             //Saving of object in a file
FileOutputStream file = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(file);
                             // Method for serialization of object
                             out.writeObject(object);
                             out.close();
                             file.close();
                             System.out.println("Object has been serialized");
                   }
                   catch(IOException ex)
                   {
                             System.out.println("IOException is caught");
                   }
                   Demo object1 = null;
                   // Deserialization
                   try
                   {
                             // Reading the object from a file
                             FileInputStream file = new FileInputStream(filename);
                             ObjectInputStream in = new ObjectInputStream(file);
                             // Method for deserialization of object
                             object1 = (Demo)in.readObject();      
                             in.close();
                             file.close();
                             System.out.println("Object has been deserialized ");
                             System.out.println("a = " + object1.a);
                             System.out.println("b = " + object1.b);
                   }                
                   catch(IOException ex)
                   {
                             System.out.println("IOException is caught");
                   }
                   catch(ClassNotFoundException ex)
                   {
                             System.out.println("ClassNotFoundException is caught");
                   } }}

Output:

Object has been serialized
Object has been  deserialized
a=1
b=today java school

No comments:

Post a Comment