Skip to main content

Object serialization in java


Serialization is the conversion of an object to a series of bytes, so that the object can be easily saved to persistent storage or streamed across a communication link. The byte stream can then be deserialized - converted into a replica of the original object.




When you want to serialize an object, that respective class should implement the marker interface serializable. It just informs the compiler that this java class can be serialized. You can tag properties that should not be serialized as transient. You open a stream and write the object into it.

Code for serialization of a java class :



Data.java

package com.codetalk.serialization;

import java.io.Serializable;

public class Data implements Serializable {
 
 private static final long serialVersionUID = 1L;
 
 private String firstName;
 private String lastName;
 
 /**
  * @return the firstName
  */
 public String getFirstName() {
  return firstName;
 }
 /**
  * @param firstName the firstName to set
  */
 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }
 /**
  * @return the lastName
  */
 public String getLastName() {
  return lastName;
 }
 /**
  * @param lastName the lastName to set
  */
 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

}


Serialization.java

package com.codetalk.serialization;

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

public class Serialization {

 public static void main(String[] args) {
  
  Data   data = new Data();
  String file = "/home/rakesh/data.txt";
  
  data.setFirstName("Rakesh KR");
  data.setLastName("KR");

  try {
   FileOutputStream   fileOut = new FileOutputStream(file);
   ObjectOutputStream out     = new ObjectOutputStream(fileOut);
   out.writeObject(data);
   out.close();
   fileOut.close();
   System.out.println("Serialized data is saved in : "+file);
  } catch(IOException i) {
   i.printStackTrace();
  } catch(Exception e){
   e.printStackTrace();
  } finally{
   data = null;
  }
 }
}


DeSerialization.java
package com.codetalk.serialization;

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

public class Deserialization {
 
 public static void main(String[] args) {

  Data   data = null;
  String file = "/home/rakesh/data.txt";
  
  try {
   FileInputStream fileIn = new FileInputStream(file);
   ObjectInputStream in   = new ObjectInputStream(fileIn);
   data = (Data) in.readObject();
   in.close();
   fileIn.close();
  } catch (IOException i) {
   i.printStackTrace();
   return;
  } catch (ClassNotFoundException c) {
   System.out.println("Data class not found");
   c.printStackTrace();
  }
  
  System.out.println("Deserializing Object...");
  System.out.println("First Name  : "+data.getFirstName());
  System.out.println("Last  Name  : "+data.getLastName());
  
 }
 
}


Output :
Deserializing Object...
First Name  : Rakesh KR
Last  Name  : KR

Comments

Popular posts from this blog

ജാവാ വളരെ സിമ്പിൾ ആണ് , പവർഫുൾ ഭയങ്കര പവർഫുൾ ആണ്.

ജാവ യെ  കുറിച്ച്  മലയാളത്തിൽ  ഡോകുമെന്റുകളോ  ബ്ലോഗ്‌കളോ  കൂടുതൽ ഇല്ലാത്തതുകൊണ്ട് എനികറിയവുന്നത് ഷെയർ ചെയ്യാം എന്ന് വിചാരിച്ചു .  ജവയെ കുറിച്ച് പറയുകയാണെങ്കിൽ  ജാവാ വളരെ സിമ്പിൾ ആണ് , പവർഫുൾ  ഭയങ്കര പവർഫുൾ ആണ്. :) 1. നമ്മടെ Sun MicroSystems 1995 ൽ തുടങ്ങി വച്ചാ സംഭവമാണിത് . 2. ഇപ്പൊ ജാവയുടെ എട്ടാമത്തെ വെർഷൻ  വരെ ഇറങ്ങികഴിഞ്ഞു . ജാവയുടെ തിയറിയെ കുറിച്ച് എനിക്ക് പറയാൻ താല്പര്യം  ഇല്ലാ, അത് അറിവിള്ളവർ നല്ലപോലെ പറഞ്ഞിടുണ്ട്. നമുക്ക് ഡയറക്റ്റ്  ആയി കോഡിംഗ് പാർട്ടിലേക്ക്  പോകാം. എങ്ങനെയാ ജാവ ഇൻസ്റ്റോൾ ചെയണ്ടേ എന്നൊക്കെ ഒന്ന് ഗൂഗിൾനോട് ചോദിച്ചാ മതി പുള്ളി പറഞ്ഞുതരും. പിന്നെ എല്ലാവരും പറയുന്നു ജാവാ Object  Oriented ആണെന്ന് . അതെ ജാവാ Object Oriented ആണ് . എന്താണ് Object ?, Object  നു വച്ചാ ഒരു സാധനം അത്രതന്നെ. ഇപ്പൊ example പറയുകയാണെങ്കിൽ Book ഒരു സാധനമാണ് . Book റ്റെ പ്രതേകത എന്തൊക്കെ അന്നെന്നുവച്ചാ ആതിനൊരു Auther ഉണ്ടാകും, അതൊരു Category ൽ പെട്ട book ആയിരിക്കും (Novel / Academic ) പിന്നെ ഓരോ bookനും അ...

Transform Ubuntu into Xubuntu

Installation 1. Open the terminal by Ctrl+Alt+T     and type    sudo apt-get install xubuntu-desktop gksu leafpad synaptic         2. Type your password & Press Enter. Now an intensive operation is being launched. Simply wait to complete the whole process. Login To Xubuntu 1. After completing the installation logout ubuntu. Note: logout not restart or shutdown. 2. In the login window click on the ubuntu logo, next to your userName & select Xubuntu Sesion 3. Enter your password and Now the Xubuntu desktop appears. :) The next thing is to clean up. Clean Up 1. Now its time to clean up, inorder to prevent system pollution problems. Note: The clean up will remove as much as possible ubuntu's desktop environment Unity. So after that you can't use Unity. 2. Open terminal by Ctrl+Alt+T and type the following  sudo apt-get remove nautilus gnome-power-manager gnome-screensaver gnome-termina*...