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

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

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

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

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

当通过下面的代码序列化之后,[color=blue]Student[/color]对象中的[color=blue]id[/color],[color=blue]name[/color]和[color=blue]sex[/color]实例变量的值都被保存到[color=blue]data.ser[/color]文件中,这样以后又可以把它从文件中读出来,重新在堆中创建原来的对象。当然保存时候不仅仅是保存对象的实例变量的值,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.安全方面的原因,比如一个对象拥有[color=blue]private[/color],[color=blue]public[/color]等field,对于一个要传输的对象,比如写到文件,或者进行[color=blue]rmi[/color]传输 等等,在序列化进行传输的过程中,这个对象的[color=blue]private[/color]等域是不受保护的。
2.资源分配方面的原因,比如[color=blue]socket[/color],[color=blue]thread[/color]类,如果可以序列化,进行传输或者保存,也无法对他们进行重新的资源分配,而且,也是没有必要这样实现。
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;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值