How to serialize the object in java?

Why we need to serialize the object in java?

We all create reusable objects which resides in memory, but those objects resides in memory as long as JVM keeps running. Sometimes we need to save the objects beyond the JVMs and use them in different JVMs. Java provides facility to flatten the objects and reuse them.

What is serialization in java?

Serialization is the process of saving an object's state in a file or memory buffer or such storage mediums or transmit it over a network connection in binary form. This process is also called as marshalling or deflating an object.

JVM Independent :

Serialized objects are JVM independent and can be re serialized in by any JVM.

Objects :

Only objects which are marked as Serializable can be persisted. The java.lang.Object class does not implement Serializable interface. Object of Swing GUI components, strings, and arrays can be made as Serializable.


Lets look at how we can Serialize and Re-serialize the object.

1. Lets create one Serializable object user which implement the Serializable interface.

import java.io.Serializable;
import java.util.Date;
import java.util.Calendar;
public class PersistantUser implements Serializable
{

private String userName;
private Date time;


public PersistantUser(String userToSet)
{
userName = userToSet;
time = Calendar.getInstance().getTime();
}

public Date getTime()
{
return time;
}
public String getUserName(){
return userName;
}
}


2. Lets see how to serialize the object we have created.
To do this we need to have java.io.ObjectOutputStream class


import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class SerializeUser {

public static void main(String[] args) {
String filename = "user.ser";
if (args.length > 0) {
filename = args[0];
}
PersistantUser user = new PersistantUser("p1_user");
System.out.println(" User "+user.getUserName()+ " created on "+user.getTime());
FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
fos = new FileOutputStream(filename);
out = new ObjectOutputStream(fos);
out.writeObject( user );
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}


Output :
User p1_user created on Sat Oct 23 13:18:16


3. Lets look at how we can re-serialize the object :


import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Calendar;

public class ReSerializeUser {

public static void main(String[] args) {
String filename = "user.ser";
if (args.length > 0) {
filename = args[0];
}
PersistantUser user = null;
FileInputStream fis = null;
ObjectInputStream in = null;
try {
fis = new FileInputStream(filename);
in = new ObjectInputStream(fis);
user = (PersistantUser) in.readObject();
in.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
// Getting back the object
System.out.println("Getting user object " + user.getUserName() + " created at "+user.getTime() +" retrieved at "+Calendar.getInstance().getTime());

}
}


Output : Getting user object p1_user created at Sat Oct 23 13:18:16 retrieved at Sat Oct 23 13:31:20
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment