Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

Serialization

How do you do Serialization, any sample codes?
what is it for too..
0
jedistar
Asked:
jedistar
  • 2
1 Solution
 
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
 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now