Header Ads

Introduction to Object Serialization in Java part-2

Hope you liked my first post on object  serialization  http://www.askqtp.com/2012/01/object-serialization-in-java.html

In this post I am going to write the practice code to get an object and its state serialized. And again we are going to read that.As Time, is a serializable object, so we will work on Time.
My object is to capture current time and save to a file.Again read from that file.

For capturing time we need to create a class.This is a separate class which will tell java that this class's object is serializable. So we use to implement that class to Serializable.

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;

public class myTime implements Serializable {
    private Date time;
    public myTime() {

public Date callTime() {
    return time;
public static void main(String[] args) {
        myTime myTime = new myTime();

Cool ...The second objective is to create a class that will create a file and write this time as a serialized object.I am going to create a file called time.ser under C drive.
There are two things important here
  • FileOutputStream
  • ObjectOutputStream

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class writeData {
    public static void main(String[] args) {
    String fileName="time.ser";
    FileOutputStream fos=null;
    ObjectOutputStream out=null;
    myTime time=new myTime();
        File file;
        file = new File("C:\\"+fileName);
        fos=new FileOutputStream(file);
        out=new ObjectOutputStream(fos);
    } catch (FileNotFoundException e) {
       // TODO
    catch (IOException e) {
      // TODO

if I compile and run this program, I am going to get a file created under C drive called time.ser.This file is the container where the serialized object is placed. This is not readable by Human.Only JVM can read this file.It is strongly advisable that please do not open or modify the file
This file contains line like---
Ԁ牳ༀ牰橯捥ㅴ洮呹浩깥좟ʜĀL琄浩瑥က橌癡⽡瑵汩䐯瑡㭥灸牳฀慪慶甮楴⹬慄整橨Ɓ奋ᥴ 砀睰 Ā㠵뎪碈
Now we need to read it...

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Calendar;
public class readData {
    public static void main(String[] args) {
        String filename = "time.ser";
        myTime time = null;
        FileInputStream fis = null;
        ObjectInputStream in = null;
        fis = new FileInputStream("C:\\" + filename);
        in = new ObjectInputStream(fis);
        time = (myTime)in.readObject();
        catch(IOException ex)
        catch(ClassNotFoundException ex)
        // print out restored time
        System.out.println("Flattened time: " + time.callTime());
        // print out the current time
        System.out.println("Current time: " + Calendar.getInstance().getTime());
This what we are going to get
project1.myTime@4741d6Flattened time: Wed Feb 01 16:56:45 IST 2012
Current time: Wed Feb 01 17:25:00 IST 2012
I serialized time at 16:56:45 and I deserialized that time at 17:25:00
Powered by Blogger.