标签:java
1.什么是java序列化
Java平台允许我们在内存中创建可复用的Java对象,但一般情况下,只有当JVM处于运行时,这些对象才可能存在,即,这些对象的生命周期不会比JVM的生命周期更长。但在现实应用中,就可能要求在JVM停止运行之后能够保存(持久化)指定的对象,并在将来重新读取被保存的对象。Java对象序列化就能够帮助我们实现该功能。
使用Java对象序列化,在保存对象时,会把其状态保存为一组字节,在未来,再将这些字节组装成对象。必须注意地是,对象序列化保存的是对象的"状态",即它的成员变量。由此可知,对象序列化不会关注类中的静态变量。
除了在持久化对象时会用到对象序列化之外,当使用RMI(远程方法调用),或在网络中传递对象时,都会用到对象序列化。Java序列化API为处理对象序列化提供了一个标准机制,该API简单易用。
2.如何序列化一个对象
一个对象能够序列化的前提是实现Serializable接口,Serializable接口没有方法,更像是个标记。有了这个标记的Class就能被序列化机制处理。
import java.io.Serializable;
class TestSerial implements Serializable {
public byte version = 100;
public byte count = 0;
} public static void main(String args[]) throws IOException {
FileOutputStream fos = new FileOutputStream("temp.out");
ObjectOutputStream oos = new ObjectOutputStream(fos);
TestSerial ts = new TestSerial();
oos.writeObject(ts);
oos.flush();
oos.close(); public static void main(String args[]) throws IOException {
FileInputStream fis = new FileInputStream("temp.out");
ObjectInputStream oin = new ObjectInputStream(fis);
TestSerial ts = (TestSerial) oin.readObject();
System.out.println("version="+ts.version);
} 3.对象的序列化格式
将一个对象序列化后是什么样子呢?打开刚才我们将对象序列化输出的temp.out文件,以16进制方式显示。内容应该如下:
AC ED 00 05 73 72 00 0A 53 65 72 69 61 6C 54 65
73 74 A0 0C 34 00 FE B1 DD F9 02 00 02 42 00 05
63 6F 75 6E 74 42 00 07 76 65 72 73 69 6F 6E 78
70 00 64
这一坨字节就是用来描述序列化以后的
TestSerial对象的,我们注意到TestSerial类中只有两个域:
public byte version = 100;
public byte count = 0;
且都是byte型,理论上存储这两个域只需要2个byte,但是实际上temp.out占据空间为51bytes,也就是说除了数据以外,还包括了对序列化对象的其他描述。
Java的序列化算法
序列化算法一般会按步骤做如下事情:
◆将对象实例相关的类元数据输出。
◆递归地输出类的超类描述直到不再有超类。
◆类元数据完了以后,开始从最顶层的超类开始输出对象实例的实际数据值。
◆从上至下递归输出实例的数据
我们用另一个更完整覆盖所有可能出现的情况的例子来说明:
class parent implements Serializable {
int parentVersion = 10;
}
class contain implements Serializable{
int containVersion = 11;
}
public class SerialTest extends parent implements Serializable {
int version = 66;
contain con = new contain();
public int getVersion() {
return version;
}
public static void main(String args[]) throws IOException {
FileOutputStream fos = new FileOutputStream("temp.out");
ObjectOutputStream oos = new ObjectOutputStream(fos);
SerialTest st = new SerialTest();
oos.writeObject(st);
oos.flush();
oos.close();
}
}
这个例子是相当的直白啦。SerialTest类实现了Parent超类,内部还持有一个Container对象。
序列化后的格式如下:
AC ED 00 05 73 72 00 0A 53 65 72 69 61 6C 54 65
73 74 05 52 81 5A AC 66 02 F6 02 00 02 49 00 07
76 65 72 73 69 6F 6E 4C 00 03 63 6F 6E 74 00 09
4C 63 6F 6E 74 61 69 6E 3B 78 72 00 06 70 61 72
65 6E 74 0E DB D2 BD 85 EE 63 7A 02 00 01 49 00
0D 70 61 72 65 6E 74 56 65 72 73 69 6F 6E 78 70
00 00 00 0A 00 00 00 42 73 72 00 07 63 6F 6E 74
61 69 6E FC BB E6 0E FB CB 60 C7 02 00 01 49 00
0E 63 6F 6E 74 61 69 6E 56 65 72 73 69 6F 6E 78
70 00 00 00 0B
我们来仔细看看这些字节都代表了啥。开头部分,见颜色:
序列化算法的第一步就是输出对象相关类的描述。例子所示对象为SerialTest类实例,因此接下来输出SerialTest类的描述。见颜色:
接下来,算法输出其中的一个域,int version=66;见颜色:
然后,算法输出下一个域,contain con = new contain();这个有点特殊,是个对象。描述对象类型引用时需要使用JVM的标准对象签名表示法,见颜色:
.接下来算法就会输出超类也就是Parent类描述了,见颜色:
下一步,输出parent类的域描述,int parentVersion=100;同见颜色:
到此为止,算法已经对所有的类的描述都做了输出。下一步就是把实例对象的实际值输出了。这时候是从parent Class的域开始的,见颜色:
还有SerialTest类的域:
再往后的bytes比较有意思,算法需要描述contain类的信息,要记住,现在还没有对contain类进行过描述,见颜色:
.输出contain的唯一的域描述,int containVersion=11;
这时,序列化算法会检查contain是否有超类,如果有的话会接着输出。
最后,将contain类实际域值输出。
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
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;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
} 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:\\person.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
System.out.println("Person--Jay,24---Written");
oos.writeObject(myPerson);
oos.flush();
oos.close();
}catch(Exception e){
e.printStackTrace();
}
}
@SuppressWarnings("resource")
public void restorePerson(){
try{
FileInputStream fls = new FileInputStream("E:\\person.txt");
ObjectInputStream ois = new ObjectInputStream(fls);
Person myPerson = (Person)ois.readObject();
System.out.println("\n---------------------\n");
System.out.println("Person --read:");
System.out.println("Name is:"+myPerson.getName());
System.out.println("Age is :"+myPerson.getAge());
}catch(Exception e){
e.printStackTrace();
}
}
}
public class SerializeXML {
public static void main(String[] args) {
SerializeXML ser = new SerializeXML();
ser.serializeToXml();
ser.deSerializeFromXml();
}
public void serializeToXml(){
Person[] myPersons = new Person[2];
myPersons[0] = new Person("Jay", 24);
myPersons[1] = new Person("Tom", 23);
XStream xStream = new XStream();
xStream.alias("Person", Person.class);
try{
FileOutputStream foStream = new FileOutputStream("E:\\persons.xml");
xStream.toXML(myPersons,foStream);
}catch(Exception e){
e.printStackTrace();
}
}
public void deSerializeFromXml(){
XStream xStream = new XStream();
xStream.alias("Person", Person.class);
Person[] myPersons = null;
try{
FileInputStream flStream = new FileInputStream("E:\\persons.xml");
myPersons = (Person[])xStream.fromXML(flStream);
if(myPersons!=null){
for(Person person:myPersons){
System.out.println(person.getName());
System.out.println(person.getAge());
}
}
}catch(Exception e){
e.printStackTrace();
}
}
} public class SerializeJSON {
public static void main(String[] args) {
SerializeJSON serializeJSON = new SerializeJSON();
serializeJSON.writeJSON();
serializeJSON.readJSON();
}
public void writeJSON(){
XStream xStream = new XStream(new JettisonMappedXmlDriver());
Person person = new Person("geniushehe", 16);
try {
FileOutputStream fos = new FileOutputStream("E:\\json.js");
xStream.setMode(XStream.NO_REFERENCES);
xStream.alias("Person", Person.class);
xStream.toXML(person, fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void readJSON(){
XStream xStream = new XStream(new JettisonMappedXmlDriver());
Person person = null;
try {
FileInputStream fis = new FileInputStream("E:\\json.js");
xStream.setMode(XStream.NO_REFERENCES);
xStream.alias("Person", Person.class);
person = (Person)xStream.fromXML(fis);
System.out.println(person.getName());
System.out.println(person.getAge());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
标签:java
原文地址:http://blog.csdn.net/jirongzi_cs2011/article/details/37775511