readObject() and writeObject() in Java Serialization

By Arvind Rai, December 18, 2022
On this page, we will learn readObject and writeObject methods in Java serialization.
1. Java classes which require user defined handling of serialization and deserialization, they need to use readObject and writeObject methods.
2. In serialization, the writeObject method writes the byte stream in physical location.
3. The readObject method is used to read byte stream from physical location and type cast to required class.
4. In default mechanism, static field and transient variable are not serialized or deserialized. If we want to serialize transient variable, we need to use readObject and writeObject methods.

Serializable Interface

The java.io.Serializable is an interface that has no methods or fields. It is a marker interface that serves only to identify the semantics of being serializable. The class that implements the Serializable interface is eligible to be serialized.
class ConcretePage implements Serializable {
   ------
} 
The sub-classes of non-serializable classes can be serialized and deserialized by implementing Serializable interface and in serialization, no data will be written for the fields of non-serializable super classes.

Custom Serialization

For custom serialization, we need to define following methods with exact signatures.
1.
private void writeObject(java.io.ObjectOutputStream out)
     throws IOException 
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.
2.
private void readObject(java.io.ObjectInputStream in)
     throws IOException, ClassNotFoundException; 
The readObject method is responsible for reading from the stream and restoring the class fields.
3.
private void readObjectNoData() 
            throws ObjectStreamException 
If the class being restored is not present in the stream being read, then readObjectNoData method is invoked instead of readObject method.

Example to use readObject() and writeObject()

ConcretePage.java
package com.concretepage;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class ConcretePage implements Serializable {
  public static final long serialVersionUID = 1L;
  private String user;
  private transient String author;

  public ConcretePage(String user, String author) {
	this.user = user;
	this.author = author;
  }

  private void writeObject(ObjectOutputStream out) throws IOException {
	out.defaultWriteObject();
	out.writeObject(this.author);
  }

  private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
	in.defaultReadObject();
	this.author = (String) in.readObject();
  }

  public String getUser() {
	return user;
  }

  public void setUser(String user) {
	this.user = user;
  }

  public String getAuthor() {
	return author;
  }

  public void setAuthor(String author) {
	this.author = author;
  }
} 
In the above class, author variable is transient. We have serialized it by implementing writeObject and readObject methods.
ConcreteMain.java
package com.concretepage;

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 ConcreteMain {
  public static void main(String... args) throws Exception, IOException {
	File f = new File("a.txt");
	ConcretePage cp = new ConcretePage("Mahesh", "Suresh");

	ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
	out.writeObject(cp);

	ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
	cp = (ConcretePage) in.readObject();
	System.out.println("After deserialization, user is " + cp.getUser() + " and author is " + cp.getAuthor());

	out.close();
	in.close();
  }
} 
Output
After deserialization, user is Mahesh and author is Suresh 

Reference

Interface Serializable
POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us