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");// 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();/* * @(#)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;}}