首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Java Serializable(序列化) 懂得和总结(摘录)

2012-10-12 
Java Serializable(序列化) 理解和总结(摘录)1、序列化是干什么的?简单说就是为了保存在内存中的各种对象的

Java Serializable(序列化) 理解和总结(摘录)
1、序列化是干什么的?
        简单说就是为了保存在内存中的各种对象的状态(也就是实例变量,不是方法),并且可以把保存的对象状态再读出来。虽然你可以用你自己的各种各样的方法来保存object states,但是Java给你提供一种应该比你自己好的保存对象状态的机制,那就是序列化。

2、什么情况下需要序列化   
        a)当你想把的内存中的对象状态保存到一个文件中或者数据库中时候;   
        b)当你想用套接字在网络上传送对象的时候;   
        c)当你想通过RMI传输对象的时候;

3、当对一个对象实现序列化时,究竟发生了什么?
        在没有序列化前,每个保存在堆(Heap)中的对象都有相应的状态(state),即实例变量(instance ariable)比如:

Student student = new Student();student.setId(1);student.setName("Harrison");student.setSex("boy");

        当通过下面的代码序列化之后,Student对象中的id,name和sex实例变量的值都被保存到data.ser文件中,这样以后又可以把它从文件中读出来,重新在堆中创建原来的对象。当然保存时候不仅仅是保存对象的实例变量的值,JVM还要保存一些小量信息,比如类的类型等以便恢复原来的对象。
// Make a FileOutputStreamFileOutputStream fos = new FileOutputStream("data.ser");// Make a ObjectOutputStreamObjectOutputStream oos = new ObjectOutputStream(fos);// Write the objectoos.writeObject(student);// Close the ObjectOutputStreamoos.close();// Close the FileOutputStreamfos.close();

4、相关注意事项
        a)序列化时,只对对象的状态进行保存,而不管对象的方法;
        b)当一个父类实现序列化,子类自动实现序列化,不需要显式实现Serializable接口;
        c)当一个对象的实例变量引用其他对象,序列化该对象时也把引用对象进行序列化;
        d)并非所有的对象都可以序列化,,至于为什么不可以,有很多原因了,比如:
            1.安全方面的原因,比如一个对象拥有private,public等field,对于一个要传输的对象,比如写到文件,或者进行rmi传输  等等,在序列化进行传输的过程中,这个对象的private等域是不受保护的。
            2.资源分配方面的原因,比如socket,thread类,如果可以序列化,进行传输或者保存,也无法对他们进行重新的资源分配,而且,也是没有必要这样实现。
5、测试代码
/* * @(#)StudentTest.java 1.0 Jun 4, 2010 */package org.asheng.model;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import org.junit.Test;/** * @author Harrison Wang * @version 1.0 */public class StudentTest {private ObjectOutputStream oos;private FileOutputStream fos;private ObjectInputStream ois;private FileInputStream fis;/** * Write the object instance * @param student * @return */public boolean writeInstance(Student instance) {try {// Make a FileOutputStreamfos = new FileOutputStream("data.ser");// Make a ObjectOutputStreamoos = new ObjectOutputStream(fos);// Write the objectoos.writeObject(instance);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {// Close the ObjectOutputStreamoos.close();// Close the FileOutputStreamfos.close();} catch (IOException e) {e.printStackTrace();}}return true;}/** * Read the object instance * @return */public Student readInstance() {Student stu = null;try {// Make a FileInputStreamfis = new FileInputStream("data.ser");// Make a ObjectInputStreamois = new ObjectInputStream(fis);// Get the Objectstu = (Student) ois.readObject();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();} finally {try {// Close the ObjectInputStreamois.close();// Close the FileInputStreamfis.close();} catch (IOException e) {e.printStackTrace();}}return stu;}@Testpublic void test() {Student student = new Student();student.setId(1);student.setName("Harrison");student.setSex("boy");boolean isWriteen = writeInstance(student);if (isWriteen == true) {// Get the student instanceStudent stu = readInstance();// Print the student nameSystem.out.println(stu.getName());}}}

/* * @(#)Student.java 1.0 Jun 4, 2010 */package org.asheng.model;import java.io.Serializable;/** * @author Harrison Wang * @version 1.0 */public class Student implements Serializable {private static final long serialVersionUID = 1L;private int id;private String name;private String sex;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}}

热点排行