How do you do Serialization, any sample codes?
what is it for too..
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Quoting from:
"We all know the Java platform allows us to create reusable objects in memory. However, all of those objects exist only as long as the Java virtual machine1 remains running. It would be nice if the objects we create could exist beyond the lifetime of the virtual machine, wouldn't it? Well, with object serialization, you can flatten your objects and reuse them in powerful ways."

It can be thought of as a way to convert Java objects into Strings - for storage, sharing across VMs, and many other uses.

Some interesting links to check out:

If you want a class of yours to be Serializable, simply have it implement the interface, which is only a marker interface.

To serialize an object, obj, to a file:
// Create an appropriate ObjectOutputStream, and then call writeObject(obj) on that stream
SomeSerializableClass obj = new SomeSerializableClass();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("somefilename"));

To de-serialize an object, obj, from a file:
// Create an appropriate ObjectInputStream, and then call readObject() on that stream
ObjectInputStream ios = new ObjectInputStream(new FileInputStream("somefilename"));
SomeSerializableClass obj = (SomeSerializableClass)ois.readObject();


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jedistar -

As to what it can be used for, I should elaborate.  Two popular uses of serialization are:

- Enabling RMI - this is how objects are sent from a local VM to the remote VM

- One way to store information (properties/settings, etc) for an application to disk (instead of creating a text-based format, simply write your setting objects straight to a file and read them in as simply)

Also, here are some more examples straight from Sun:

From JDK 1.4 you can serialize in XML also:

import java.util.Vector;
import java.beans.XMLEncoder;

public class Dummy {
    public static void main(String[] args)
        final Vector toSerialize = new Vector();
        toSerialize.add("This is string");
        toSerialize.add(new Integer(1));
        toSerialize.add(new Long(1));
        toSerialize.add(new Double(1));

        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        XMLEncoder encoder = new XMLEncoder(out);



<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_06" class="java.beans.XMLDecoder">
 <object class="java.util.Vector">
  <void method="add">
   <string>This is string</string>
  <void method="add">
  <void method="add">
  <void method="add">
FileOutputStream fileOut = new FileOutputStream("MyFile.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.