TestingDocs.com
    Software Testing website
    • Automation
      • Selenium
      • JBehave Framework
    • Tutorials
      • MySQL Tutorials
      • Testlink
      • Maven
      • Git
    • IDEs
      • IntelliJ IDEA
      • Eclipse
    • Flowcharts
      • Flowgorithm
      • Raptor
    • About

    Java Tutorials

    Java Serialization and De-serialization

    Overview

    Java serialization is a process of writing the state of the object into a writable byte stream. Serializable classes can be written to a file on your computer. The reverse process of serialization is called de-serialization. i.e restoring the state of the object from the byte stream.

     

    Java Serialization

    Serialization of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or de-serialized. Serializable is a marker interface.

    Let’s understand the concept with an example. Serialization of book object to stream:

    objectOutputStream.writeObject(book1);

    De-serialization of book object from the stream:

    objectInputStream.readObject();

    The writeObject() method is responsible for writing the state of the object for its particular class so that the corresponding readObject() method can restore it.

    Consider the following Java class Book with the following instance variables.

    Book Java Class Serialization

     

    Book Class

    import java.io.Serializable;
    
    public class Book implements Serializable {
    
     private static final long serialVersionUID = 1L;
     //Instance variables of the class
     private String title;
     private String publication;
     double price;
    
     // Constructor
     public Book(String title, String publication,double price) {
     this.title=title;
     this.publication=publication;
     this.price=price;
     }
    
     //tostring method
     public String toString() {
     return "Book Title:" + title + "\n"
     + "Publication:" + publication + "\n"
     + "Price: $" + price;
     }
    
    }

     

    Driver Class

    Driver class is used to demonstrate the serialization and de-serialization process of the Book class.

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    
     public class Driver {
    
     public static void main(String[] args) {
     Driver driver= new Driver();
     FileOutputStream fileOuputStream = null;
     ObjectOutputStream objectOutputStream = null;
    
     FileInputStream fileInputStream = null;
     ObjectInputStream objectInputStream = null;
     String filename = "bookObjects.data";
     // Instantiate objects
     Book book1=new Book("Java Made Easy","XYZ Publication",23.95);
     Book book2=new Book("Testing Made Easy","ABC Publication",95.0);
    
     try {
     fileOuputStream = new FileOutputStream(filename);
     objectOutputStream = new ObjectOutputStream(fileOuputStream);
     objectOutputStream.writeObject(book1);
     objectOutputStream.writeObject(book2);
    
     } catch (Exception ex) {
     ex.printStackTrace();
     } finally {
     if (fileOuputStream != null) {
     try {
     fileOuputStream.close();
     } catch (IOException e) {
     e.printStackTrace();
     }
     }
    
     if (objectOutputStream != null) {
     try {
     objectOutputStream.close();
     } catch (IOException e) {
     e.printStackTrace();
     }
     }
    
     }
     try {
     fileInputStream = new FileInputStream(new File(filename));
     objectInputStream = new ObjectInputStream(fileInputStream);
    
     // Read objects
     Book b1 = (Book) objectInputStream.readObject();
     Book b2 = (Book) objectInputStream.readObject();
    
     System.out.println("Books read from file:");
     System.out.println(b1.toString());
     System.out.println(b2.toString());
    
    
     } catch (Exception ex) {
     ex.printStackTrace();
    
     } finally {
    
     if (fileInputStream != null) {
     try {
     fileInputStream.close();
     } catch (IOException e) {
     e.printStackTrace();
     }
     }
    
     if (objectInputStream != null) {
     try {
     objectInputStream.close();
     } catch (IOException e) {
     e.printStackTrace();
     }
     }
    
     }
     }
     }

     

    Program Output

    Books read from file:
    Book Title:Java Made Easy
    Publication:XYZ Publication
    Price: $23.95
    Book Title:Testing Made Easy
    Publication:ABC Publication
    Price: $95.0

     

     

    —

    Java Tutorials

    Java Tutorial on this website:

    https://www.testingdocs.com/java-tutorial/

    For more information on Java, visit the official website :

    https://www.oracle.com/java/

    Related Posts

    Java Performance

    Java Tutorials /

    Improving Java Performance with Multithreading

    Download Greenfoot Windows

    Java Tutorials /

    Download & Install Greenfoot on Windows

    Java Tutorials /

    Java Static Code Analysis

    Java Tutorials /

    Java Testing Tools

    Java Tutorials /

    Handle Multiple Exceptions in Java

    ‹ Java Control Panel on Windows› Java Nested classes

    Recent Posts

    • Running Tests in Parallel with Selenium Grid
    • Advanced Selenium Features
    • Locating Web Elements
    • Running the Test Script
    • Writing Your First Selenium Test Script
    • Getting Started with Selenium Automation Testing
    • Setting Up the Environment
    • How can you monitor the Quality Assurance Audit?
    • Leveraging LambdaTest with Appium 2.0
    • Appium 2.0 Plugins ListAppium 2.0 Plugins
    • Touch Actions and Multi-Touch Actions
    • Changes in Drivers and Classes
    • Appium Inspector
    • Capabilities in Appium 2.0
    • Appium 2.0 Driver ListAppium 2.0 Driver Installation & Management
    CyberLink Multimedia Software

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com