Posted on 2006-04-09
Last Modified: 2010-03-31
How do you do Serialization, any sample codes?
what is it for too..
Question by:jedistar

    Accepted Solution

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


    Expert Comment

    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:

    LVL 2

    Expert Comment

    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">
    LVL 5

    Expert Comment

    FileOutputStream fileOut = new FileOutputStream("MyFile.ser");
    ObjectOutputStream out = new ObjectOutputStream(fileOut);

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One Java Help from Top Experts

    Solve your toughest problems, fast.
    Java experts are online now and ready to help you.

    Suggested Solutions

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now