How to use serialization with JAVA? Serialize objects


Serialization can be considered as saving the state of any class instances into a file or a stream.

Firstly, Serializable objects must implement the Serializable interface.

Lets use the following code for serialization.

Message.java class will be serialized by the program so that it must implement Serializable interface

package serializationexample;

import java.io.Serializable;

/**
*
* @author tunatore
*/
public class Message implements Serializable{

private int messageId; //–>available for serialization
private String code; //–>available for serialization
private String content; //–>available for serialization
private transient String privateInfo;
//this field does not allow serialization
//the value stored inside this field wont be available to read
//by an another program
//transient keyword should be used to achieve this kind of operation

/**
* @return the messageId
*/
public int getMessageId() {
return messageId;
}

/**
* @param messageId the messageId to set
*/
public void setMessageId(int messageId) {
this.messageId = messageId;
}

/**
* @return the code
*/
public String getCode() {
return code;
}

/**
* @param code the code to set
*/
public void setCode(String code) {
this.code = code;
}

/**
* @return the content
*/
public String getContent() {
return content;
}

/**
* @param content the content to set
*/
public void setContent(String content) {
this.content = content;
}

/**
* @return the privateInfo
*/
public String getPrivateInfo() {
return privateInfo;
}

/**
* @param privateInfo the privateInfo to set
*/
public void setPrivateInfo(String privateInfo) {
this.privateInfo = privateInfo;
}
}

Main.java class will handle all of serialization and deserialization processes

package serializationexample;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
*
* @author tunatore
*/
public class Main {

public static void main(String[] args) {

Message message = new Message();
message.setMessageId(0);
message.setCode(“000000”);
message.setContent(“SUCCESS MESSAGE”);
message.setPrivateInfo(“PRIVATE INFO WILL BE HERE”);

System.out.println(“BEFORE SERIALIZATION”);
System.out.println(“Message ID: ” + message.getMessageId());
System.out.println(“Message code: ” + message.getCode());
System.out.println(“Message content: ” + message.getContent());
System.out.println(“Message private info: ” + message.getPrivateInfo());

serializeMessageObject(message);

Message afterDeSerializeMessage = deserializeMessageObject();

System.out.println(“\nAFTER DESERIALIZATION”);
System.out.println(“Message ID: ” + afterDeSerializeMessage.getMessageId());
System.out.println(“Message code: ” + afterDeSerializeMessage.getCode());
System.out.println(“Message content: ” + afterDeSerializeMessage.getContent());
System.out.println(“Message private info: ” + afterDeSerializeMessage.getPrivateInfo());
//transient field will be NULL because it was not serialized by the program
}
public static void serializeMessageObject(Message message) {
try {
FileOutputStream fileOutputStream = new FileOutputStream(new File(“serialized.file”));
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(message);
objectOutputStream.close();
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
public static Message deserializeMessageObject() {
Message message = null;
try {
FileInputStream fileInputStream = new FileInputStream(new File(“serialized.file”));
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
message = (Message) objectInputStream.readObject();
objectInputStream.close();
} catch (Exception e) {
e.printStackTrace(System.out);
}
return message;
}

}

Output of the program

run:
BEFORE SERIALIZATION
Message ID: 0
Message code: 000000
Message content: SUCCESS MESSAGE
Message private info: PRIVATE INFO WILL BE HERE

AFTER DESERIALIZATION
Message ID: 0
Message code: 000000
Message content: SUCCESS MESSAGE
Message private info: null
BUILD SUCCESSFUL (total time: 2 seconds)

The program throws an exception if you don’t implement the Serializable interface by Message class.

 java.io.NotSerializableException: serializationexample.Message

at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
Exception in thread “main” java.lang.NullPointerException
at serializationexample.Main.serializeMessageObject(Main.java:44)
at serializationexample.Main.main( at serializationexample.Main.main(Main.java:29)
Main.java:34)
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: serializationexample.Message
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1332)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
at serializationexample.Main.deserializeMessageObject(Main.java:55)
at serializationexample.Main.main(Main.java:31)
Caused by: java.io.NotSerializableException: serializationexample.Message
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330)
at serializationexample.Main.serializeMessageObject(Main.java:44)
at serializationexample.Main.main(Main.java:29)

Download Source

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s