Java serialization and deserialization (practice)
Basic concepts:
Serialization is the process of converting an object's state into a format that can be persisted or transmitted. Relative to serialization is deserialization, which transforms a stream into an object. Together, these two processes make it easy to store and transfer data.
I saw it in a book yesterday, practiced it, serialized it as a generic file, and serialized it as an XML file (using XStream).
The Person.java code for the entity class used for serialization is as follows (remember to implement the Serializable interface):
Import java.io.Serializable;
@SuppressWarnings ("Serial")
public class person implements serializable{
private String name;
private int age;
Public person () {
}
Public person (String str, int n) {
System.out.println ("Inside person ' s Constructor");
name = str;
Age = N;
}
String GetName () {
return name;
}
int Getage () {
return age;
}
}
Serialized and deserialized into a generic file, the code for the Serializetoflatfile.java class is as follows:
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.ObjectInputStream;
Import Java.io.ObjectOutputStream;
public class Serializetoflatfile {
public static void Main (string[] args) {
Serializetoflatfile ser = new Serializetoflatfile ();
Ser.saveperson ();
Ser.restoreperson ();
}
public void Saveperson () {
Person MyPerson = new Person ("Jay", 24);
try {
FileOutputStream fos = new FileOutputStream ("E:\\workspace\\2010_03\\src\\myperson.txt");
ObjectOutputStream oos = new ObjectOutputStream (FOS);
System.out.println ("person--jay,24---written");
System.out.println ("Name is:" +myperson.getname ());
System.out.println ("Age is:" +myperson.getage ());
Oos.writeobject (MyPerson);
Oos.flush ();
Oos.close ();
} catch (Exception e) {
Todo:handle exception
E.printstacktrace ();
}
}
public void Restoreperson () {
try {
FileInputStream fis = new FileInputStream ("E:\\workspace\\2010_03\\src\\myperson.txt");
ObjectInputStream ois = new ObjectInputStream (FIS);
Person MyPerson = (person) ois.readobject ();
System.out.println ("\ n--------------------\ n");
System.out.println ("person--jay,24---restored");
System.out.println ("Name is:" +myperson.getname ());
System.out.println ("Age is:" +myperson.getage ());
} catch (Exception e) {
Todo:handle exception
E.printstacktrace ();
}
}
}
Run the result as (console output), and of course you can see that the MyPerson.txt file has been generated:
Inside person ' s Constructor
person--jay,24---written
Name Is:jay
Age Is:24
--------------------
person--jay,24---restored
Name Is:jay
Age Is:24
Serialized and deserialized into an XML file, I used the xstream to serialize and need to introduce support for the Xstream-1.3.1.jar package,
The jar can be downloaded at http://xstream.codehaus.org/download.html and then introduced into build path in eclipse.
The code for Serialize.java is as follows:
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import com.thoughtworks.xstream.*;
public class Serialize {
public static void Main (string[] args) {
Serialize ser = new Serialize ();
Ser.serializetoxml ();
Ser.deserializefromxml ();
}
public void Serializetoxml () {
person[] MyPerson = new person[2];
Myperson[0] = new Person ("Jay", 24);
MYPERSON[1] = new Person ("Tom", 23);
XStream XStream = new XStream ();
try {
FileOutputStream fos = new FileOutputStream ("E:\\workspace\\2010_03\\src\\myperson.xml");
Xstream.toxml (Myperson,fos);
} catch (FileNotFoundException ex) {
Ex.printstacktrace ();
}
System.out.println (Xstream.toxml (MyPerson));
}
public void Deserializefromxml () {
XStream xs = new XStream ();
person[] MyPerson = null;
try {
FileInputStream fis = new FileInputStream ("E:\\workspace\\2010_03\\src\\myperson.xml");
Myperson= (person[]) xs.fromxml (FIS);
if (MyPerson! = null)
{
int len = myperson.length;
for (int i=0;i<len;i++)
{
System.out.println (Myperson[i].getname ());
System.out.println (Myperson[i].getage ());
}
}
} catch (FileNotFoundException ex) {
Ex.printstacktrace ();
}
}
}
Run the result as (console output), and of course you can see that the Myperson.xml file has been generated:
Inside person ' s Constructor
Inside person ' s Constructor
<Person-array>
<Person>
<name>Jay</name>
<age>24</age>
</Person>
<Person>
<name>Tom</name>
<age>23</age>
</Person>
</Person-array>
Jay
24
Tom
23
Java serialization and deserialization (practice)