Cool Penguin

Simple developer guides

Java

More

on CoolPenguin.net:

Serialize objects in java

Serialization is the process of converting an object into a sequence of bits, so that it can be persisted on a storage medium such as a file or a memory buffer. This guide shows how to make a java class serializable and how to serialize/deserialize java objects and save/load them into files.

Our example class:

class Person {
    private String firstName;
    private String lastName;

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}	

The only change we need is implementing the Serializable interface. This is an empty, tagging interface, no need to implement any additional methods:

import java.io.Serializable;

class Person implements Serializable {
    private String firstName;
    private String lastName;

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

ObjectSerializer is an example class, shows how to perform object serialization in Java. The ObjectSerializer serializes or deserializes a serializable java object and write / read it to a file.

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

class ObjectSerializer {
    public void serializeObject(String fileName, Object obj) {
        ObjectOutputStream out = null;
        try {
            FileOutputStream fos = new FileOutputStream(fileName);
            out = new ObjectOutputStream(fos);
            out.writeObject(obj);
            out.close();
        } catch (IOException ex) {
            throw new RuntimeException("Serialization failed", ex);
        } finally {
            try {
                if(out != null) out.close();
            } catch(Exception e) {
                // do nothing
            }
        }

    }

    public Object deSerializeObject(String fileName) {
        Object obj;
        ObjectInputStream in = null;
        try {
            FileInputStream fis = new FileInputStream(fileName);
            in = new ObjectInputStream(fis);
            obj = in.readObject();
            return obj;
        }
        catch (Exception e) {
            throw new RuntimeException("De-serialization failed", e);
        } finally {
            try {
                if(in != null)
                    in.close();
            } catch(Exception e) {
                // do nothing
            }
        }
    }
}

Using the ObjectSerializer:

public class Test {
    public static void main(String[] args) {
        // writing to a file
        Person person = new Person("John", "Doe");
        ObjectSerializer serializer = new ObjectSerializer();
        serializer.serializeObject("person.dat", person);

        // reading from a file
        Person personFromFile = (Person) serializer.deSerializeObject("person.dat");
    }
}
No comments posted to this page
Your name

Your comment