Friday, December 6, 2013

Save Object State - How To Use Java Serialization

Object serialization in Java is one of the advanced topic in Java. In this tutorial, I'm going to show you the basic on how to serialize a simple object and then deserialize it later.

First, you want to create a simple object in which you wanted to be serialized. By the way, when I say serialize, Java will store the "state" of the particular object in bytes like saving it into a file.

Please copy & paste the source code below:

public class Hero {
 
 private int energy, life, locationx, locationy = 0;

 public Hero(){
  
 }
 
 public void setEnergy(int e){
  energy = e;
 }
 
 public void setLife(int l){
  life = l;
 }
 
 public void setLocation(int x, int y){
  locationx = x;
  locationy= y;
 }
 
 public int getLocationX() { return locationx; }
 public int getLocationY() { return locationy; }
 public int getLife() { return life; }
 public int getEnergy() { return energy; }

}

Our hero class will be serialize by creating an instance of it and using the ObjectOutputStream bundled in the Java foundation class. See example below to change the state of the Hero class and save it to a file called "hero.dat":

public class Main {
 
 public static void main(String args[]){
  Hero hero = new Hero();
  hero.setLife(100);
  hero.setEnergy(99);
  hero.setLocation(1,1);
  
  try {
         FileOutputStream fOut = new FileOutputStream("C:\\hero.dat");
         ObjectOutputStream oOut = new ObjectOutputStream(fOut);
         oOut.writeObject(e);
         oOut.close();
         fOut.close();
         System.out.printf("Success! Object has been saved.");
     }catch(Exception e){
           e.printStackTrace();
     }
 }
}
Now check out your C:\ directory and most certainly you will find a file named hero.dat which our Hero object has been stored and saved.


Now you wanted to "load" the state of our Hero class back to your application probably because you wanted to change the state. This is where the ObjectInputStream class comes into play. To deserialize our object, please run this example code given below:


public class Main {
 
 public static void main(String args[]){
  Hero hero = null;
  
  
  try {
         FileInputStream fIn = new FileInputStream("C:\\hero.dat");
         ObjectInputStream oIn = new ObjectInputStream(fIn);
         hero = (Hero) oIn.readObject();
         fIn.close();
         oIn.close();
         System.out.printf("Success! Object has been restored.");
     }catch(Exception e){
           e.printStackTrace();
     }
     
     System.out.println("Hero Life: " + hero.getLife());
     System.out.println("Hero Energy: " + hero.getEnergy());
     System.out.println("Hero X: " + hero.getLocationX());
     System.out.println("Hero Y: " + hero.getLocationY());
 }
}

Voila! You just made your first Java serialization/deserialization tutorial. I hope you enjoyed my post and thank you for reading my tutorials. Please comment below if you have some questions regarding this post.
Have a nice day!

No comments:

Post a Comment