Serialization

How do you do Serialization, any sample codes?
what is it for too..
jedistarAsked:
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.

astophelesCommented:
Quoting from: http://java.sun.com/developer/technicalArticles/Programming/serialization/
"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:
http://www.devx.com/Java/Article/9931
http://java.sun.com/developer/technicalArticles/Programming/serialization/
http://java.sun.com/docs/books/tutorial/essential/io/serializing.html

If you want a class of yours to be Serializable, simply have it implement the java.io.Serializable 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"));
oos.writeObject(obj);

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();

Cheers,
Astopheles
0

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
astophelesCommented:
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:
http://java.sun.com/j2se/1.5.0/docs/guide/serialization/examples/index.html

Cheers,
Astopheles
0
borislavmarkovCommented:
From JDK 1.4 you can serialize in XML also:


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

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);
        encoder.writeObject(toSerialize);
        encoder.flush();

        System.out.println("out:\n"+out.toString());

    }
}


produces:
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>
  <void method="add">
   <int>1</int>
  </void>
  <void method="add">
   <long>1</long>
  </void>
  <void method="add">
   <double>1.0</double>
  </void>
 </object>
0
mrigankCommented:
FileOutputStream fileOut = new FileOutputStream("MyFile.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(objectToBeSerialized);
out.close();
fileOut.close();
0
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
Java

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.