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

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

Exceptions_In_Java

Java Tutorials /

Exceptions in Java Programs

‹ Java Control Panel on Windows› Java Nested classes

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version