serialize [object] and deserialize in Java[TM]

“can i store my object into file?”
“how to transmit an object over the network”

in java programming, you can write or transmit your object over the network, how come? java provides an object serialization which ‘converts’ an object into a streaming bits, then you can do many things with the stream.
“could you give me some example?”, okay, in this session i will show you how to serialize objects and deserialize it back.
this are term people used to know:
object -> serialize / deflate / marshalling -> streaming bits
streaming bits -> deserialize / inflate / unmarshalling – > objects
one thing you have to remember is when you want to make a serializable class, make sure that you ‘flag’ it by implements java.io.Serializable interface.
the scenario of the sample is like this:
there are three classes:
SerializableObject: the class which object will be serialized
ObjectDeflater: object to streaming bit ‘converter’
ObjectInflater: streaming bit to object ‘converter’

this is very simple, just create a new project [i use JDK 1.6 update 10 and NetBeans 6.5 IDE] then create the three required classes. after that, write down these codes:

code for SerializableObject:

package javaserialization;
import java.io.Serializable;

public class SerializableObject implements Serializable {

 private final int DEFAULT_ID = 0;
 private final String DEFAULT_NAME = "DEFAULT_NAME";
 private final String DEFAULT_MESSAGE = "DEFAULT_MESSAGE";
 private int objectId;
 private String objectName;
 private String objectMessage;

 public SerializableObject() {
 objectId = DEFAULT_ID;
 objectName = DEFAULT_NAME;
 objectMessage = DEFAULT_MESSAGE;
 }

 public SerializableObject(
 int _objectId,
 String _objectName,
 String _objectMessage) {
 objectId = _objectId;
 objectName = _objectName;
 objectMessage = _objectMessage;
 }

 public int getObjectId() {
 return objectId;
 }

 public void setObjectId(int objectId) {
 this.objectId = objectId;
 }

 public String getObjectMessage() {
 return objectMessage;
 }

 public void setObjectMessage(String objectMessage) {
 this.objectMessage = objectMessage;
 }

 public String getObjectName() {
 return objectName;
 }

 public void setObjectName(String objectName) {
 this.objectName = objectName;
 }

 public void printInfo() {
 System.out.println(
 objectId + "\n" +
 objectName + "\n" +
 objectMessage + "\n");
 }
}

code for ObjectDeflater:

package javaserialization;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class ObjectDeflater {

 private String filePath;
 private File file;
 private FileOutputStream fos;
 private ObjectOutputStream oos;

 public ObjectDeflater(String _filePath) {
 filePath = _filePath;
 }

 private void allocate() throws IOException {
 file = new File(filePath);
 fos = new FileOutputStream(file);
 oos = new ObjectOutputStream(fos);
 }

 private void dealocate() {
 try {
 oos.close();
 } catch (IOException ioe) {
 ioe.printStackTrace();
 }
 }

 public void deflateObject(int _numberOfObject) throws IOException {
 allocate();

 SerializableObject so = null;
 for (int objCounter = 0; objCounter < _numberOfObject; ++objCounter) {
 so = new SerializableObject(objCounter, "my name is: " + objCounter, "my message is: " + objCounter);
 oos.writeObject(so);
 }

 dealocate();
 }
}

code for ObjectInflater:

package javaserialization;

import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class ObjectInflater {

 private String filePath;
 private File file;
 private FileInputStream fis;
 private ObjectInputStream ois;

 public ObjectInflater(String _filePath) {
 filePath = _filePath;
 }

 private void allocate() throws IOException {
 file = new File(filePath);
 fis = new FileInputStream(file);
 ois = new ObjectInputStream(fis);
 }

 private void dealocate() {
 try {
 ois.close();
 } catch (IOException ioe) {
 ioe.printStackTrace();
 }
 }

 public void inflateObject() throws IOException, ClassNotFoundException {
 allocate();

 SerializableObject so = null;
 try {
 while ((so = (SerializableObject) ois.readObject()) != null) {
 so.printInfo();
 }
 } catch (EOFException eofe) {
 // reaches end of file
 // no action required
 // leave it blank
 }

 dealocate();
 }
}

to test the project, write this code in the Main class:

package javaserialization;

public class Main {

 public static void main(String[] args) {
 // your file path, it must be exists
 String filePath = "D:\\tutorials\\JavaSerialization\\objectschest.txt";
 ObjectDeflater od = null;
 ObjectInflater oi = null;

 try {
 od = new ObjectDeflater(filePath);
 od.deflateObject(5);
 } catch (Exception ex) {
 ex.printStackTrace();
 }

 try {
 oi = new ObjectInflater(filePath);
 oi.inflateObject();
 } catch (Exception ex) {
 ex.printStackTrace();
 }
 }
}

run the project then discover what is happening…
have a good day,

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