`
asheng
  • 浏览: 12490 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
最近访客 更多访客>>
社区版块
存档分类
最新评论

Java Serializable(序列化) 理解和总结(摘录)

    博客分类:
  • Java
阅读更多
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,namesex实例变量的值都被保存到data.ser文件中,这样以后又可以把它从文件中读出来,重新在堆中创建原来的对象。当然保存时候不仅仅是保存对象的实例变量的值,JVM还要保存一些小量信息,比如类的类型等以便恢复原来的对象。
			// Make a FileOutputStream
			FileOutputStream fos = new FileOutputStream("data.ser");
			// Make a ObjectOutputStream
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			// Write the object
			oos.writeObject(student);
			// Close the ObjectOutputStream
			oos.close();
			// Close the FileOutputStream
			fos.close();

4、相关注意事项
        a)序列化时,只对对象的状态进行保存,而不管对象的方法;
        b)当一个父类实现序列化,子类自动实现序列化,不需要显式实现Serializable接口;
        c)当一个对象的实例变量引用其他对象,序列化该对象时也把引用对象进行序列化;
        d)并非所有的对象都可以序列化,,至于为什么不可以,有很多原因了,比如:
            1.安全方面的原因,比如一个对象拥有private,public等field,对于一个要传输的对象,比如写到文件,或者进行rmi传输  等等,在序列化进行传输的过程中,这个对象的private等域是不受保护的。
            2.资源分配方面的原因,比如socketthread类,如果可以序列化,进行传输或者保存,也无法对他们进行重新的资源分配,而且,也是没有必要这样实现。
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 FileOutputStream
			fos = new FileOutputStream("data.ser");
			// Make a ObjectOutputStream
			oos = new ObjectOutputStream(fos);
			// Write the object
			oos.writeObject(instance);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				// Close the ObjectOutputStream
				oos.close();
				// Close the FileOutputStream
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return true;
	}

	/**
	 * Read the object instance
	 * @return
	 */
	public Student readInstance() {
		Student stu = null;
		try {
			// Make a FileInputStream
			fis = new FileInputStream("data.ser");
			// Make a ObjectInputStream
			ois = new ObjectInputStream(fis);
			// Get the Object
			stu = (Student) ois.readObject();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				// Close the ObjectInputStream
				ois.close();
				// Close the FileInputStream
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return stu;
	}

	@Test
	public 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 instance
			Student stu = readInstance();
			// Print the student name
			System.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;
	}
	
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics