分类: Java编程
http://blog.csdn.net/hanshileiai/article/details/67198202011-08-26 09:57
80人阅读
评论(0)
收藏
举报
1、掌握对象序列化的作用
2、掌握Serializable 接口的作用
3、可以使用ObjectOutputStream 进行对象序列化操作
4、可以使用ObjectInputStream 进行对象的反序列化操作
5、掌握Externalizable 接口的作用及与Serializable 接口的实现区别
6、掌握transient 关键字的作用
7、可以序列化一组对象
1、什么对象序列化
一个对象产生之后实际上是在内存中为其开辟了一个在存储空间,方便存储信息。
一个类不能平白无故的被序列化。
但是,在此接口中没有任何一个方法,此接口属于一个标识接口,表示具备了某种能力。
例如:现在定义一个类,此类可以被序列化
定义可序列化的类
- import java.io.Serializable ;
- public class Person implements Serializable{
- private String name ; // 声明name属性,但是此属性不被序列化
- private int age ; // 声明age属性
- public Person(String name,int age){ // 通过构造设置内容
- this.name = name ;
- this.age = age ;
- }
- public String toString(){ // 覆写toString()方法
- return "姓名:" + this.name + ";年龄:" + this.age ;
- }
- };
但是在进行序列化或反序列化操作的时候,对于不同的 JDK 版本,实际上会出现版本的兼容问题。
- import java.io.Serializable ;
- public class Person implements Serializable{
- private static final long serialVersionUID = 1l;
- private String name ; // 声明name属性,但是此属性不被序列化
- private int age ; // 声明age属性
- public Person(String name,int age){ // 通过构造设置内容
- this.name = name ;
- this.age = age ;
- }
- public String toString(){ // 覆写toString()方法
- return "姓名:" + this.name + ";年龄:" + this.age ;
- }
- };
private static final long serialVersionUID = 1l;
2、对象的序列化及反序列化操作
对象序列化依靠ObjectOutputStream 对象反序列化依靠ObjectInputStream.
2.1 序列化:ObjectOutStream
- import java.io.File ;
- import java.io.FileOutputStream ;
- import java.io.OutputStream ;
- import java.io.ObjectOutputStream ;
- public class SerDemo01{
- public static void main(String args[]) throws Exception {
- File f = new File("D:" + File.separator + "test.txt") ; // 定义保存路径
- ObjectOutputStream oos = null ; // 声明对象输出流
- OutputStream out = new FileOutputStream(f) ; // 文件输出流
- oos = new ObjectOutputStream(out) ;
- oos.writeObject(new Person("张三",30)) ; // 保存对象
- oos.close() ; // 关闭
- }
- };
所有的对象拥有各自的属性值,但是所有的方法都是公共的,所以序列化对象的时候实际上序列化的就是属性。
2.2 反序列化:ObjectInputStream
- import java.io.File ;
- import java.io.FileInputStream ;
- import java.io.InputStream ;
- import java.io.ObjectInputStream ;
- public class SerDemo02{
- public static void main(String args[]) throws Exception {
- File f = new File("D:" + File.separator + "test.txt") ; // 定义保存路径
- ObjectInputStream ois = null ; // 声明对象输入流
- InputStream input = new FileInputStream(f) ; // 文件输入流
- ois = new ObjectInputStream(input) ; // 实例化对象输入流
- Object obj = ois.readObject() ; // 读取对象
- ois.close() ; // 关闭
- System.out.println(obj) ;
- }
- };
如果一个类实现了Serializable 接口,则肯定此类可以被序列化下来,那么也就意味着此类多了一项功能,可以被序列化,那么让所有的类都实现此接口是不是更好啊?因为JDK是会不断升级的,现在Serializable 接口中没有任何定义,那么以后呢?
3、Externalizable 接口
使用Serilizable 接口可以方便的序列化一个对象,但是在序列化操作中也提供了另外一种序列化机制——Externalizable 接口。
定义:
public interface Externalizable extends Serializable
方法:
写入:void writeExternal(ObjectOutput out)throws IOException
读取:void readExternal(ObjectInput in)throws IOException,ClassNotFoundException
利用此接口修改之前的程序
- import java.io.Externalizable ;
- public class Person implements Externalizable{
- private static final long serialVersionUID = 1l;
- private String name ; // 声明name属性
- private int age ; // 声明age属性
- public Person(String name,int age){ // 通过构造设置内容
- this.name = name ;
- this.age = age ;
- }
- public String toString(){ // 覆写toString()方法
- return "姓名:" + this.name + ";年龄:" + this.age ;
- }
- public void writeExternal(ObjectOutput out) throws IOException{
- out.writeObject(this.name); //保存姓名属性
- out.writeInt(this.age); //保age属性
- }
- public void readExternal(ObjectInput in) throws IOException,ClassNotFoundException{
- this.name = in.readObject(); //读取姓名
- this.age = in.readInt(); //读取年龄
- }
- };
- import java.io.File ;
- import java.io.IOException ;
- import java.io.FileOutputStream ;
- import java.io.OutputStream ;
- import java.io.ObjectOutputStream ;
- import java.io.FileInputStream ;
- import java.io.InputStream ;
- import java.io.ObjectInputStream ;
- public class SerDemo03{
- public static void main(String args[]) throws Exception{
- //ser() ;
- dser() ;
- }
- public static void ser() throws Exception {
- File f = new File("D:" + File.separator + "test.txt") ; // 定义保存路径
- ObjectOutputStream oos = null ; // 声明对象输出流
- OutputStream out = new FileOutputStream(f) ; // 文件输出流
- oos = new ObjectOutputStream(out) ;
- oos.writeObject(new Person("张三",30)) ; // 保存对象
- oos.close() ; // 关闭
- }
- public static void dser() throws Exception {
- File f = new File("D:" + File.separator + "test.txt") ; // 定义保存路径
- ObjectInputStream ois = null ; // 声明对象输入流
- InputStream input = new FileInputStream(f) ; // 文件输入流
- ois = new ObjectInputStream(input) ; // 实例化对象输入流
- Object obj = ois.readObject() ; // 读取对象
- ois.close() ; // 关闭
- System.out.println(obj) ;
- }
- };
在使用Externalizable 接口的时候需要在被序列化的类中定义一个无参构造,因为此接口在进行反序列化的时候,会先使用类中的无参构造方法为其进行实例化,之后再将内容分别设置到属性之中,修改Person 类:
- import java.io.Externalizable;
- import java.io.*;
- public class Person implements Externalizable{
- private static final long serialVersionUID = 1L;
- private String name; //声明name属性
- private int age; //声明age属性
- public Person(){} //无参构造
- public Person(String name, int age){
- this.name = name;
- this.age = age;
- }
- public String toString(){ //覆写toString()方法
- return "姓名:" + this.name + ":年龄:" + this.age;
- }
- public void writeExternal(ObjectOutput out)
- throws IOException{
- out.writeObject(this.name); //保存姓名属性
- out.writeInt(this.age); //保存age属性
- }
- public void readExternal(ObjectInput in)
- throws IOException, ClassNotFoundException{
- this.name = (String)in.readObject(); //读取姓名
- this.age = in.readInt(); //读取年龄
- }
- }
在开发中使用Serialzable 接口是最多的。而Externalizable 接口基本上是不会出现的。
在序列化操作的时候,如果某个属性不希望被序列化下来,则可以直接使用transient 关键字声明。
- import java.io.Serializable ;
- public class Person implements Serializable{
- private String name ; // 声明name属性,但是此属性不被序列化
- private int age ; // 声明age属性
- public Person(String name,int age){ // 通过构造设置内容
- this.name = name ;
- this.age = age ;
- }
- public String toString(){ // 覆写toString()方法
- return "姓名:" + this.name + ";年龄:" + this.age ;
- }
- };
操作代码:
- import java.io.File ;
- import java.io.IOException ;
- import java.io.FileOutputStream ;
- import java.io.OutputStream ;
- import java.io.ObjectOutputStream ;
- import java.io.FileInputStream ;
- import java.io.InputStream ;
- import java.io.ObjectInputStream ;
- public class SerDemo04{
- public static void main(String args[]) throws Exception{
- ser() ;
- dser() ;
- }
- public static void ser() throws Exception {
- File f = new File("D:" + File.separator + "test.txt") ; // 定义保存路径
- ObjectOutputStream oos = null ; // 声明对象输出流
- OutputStream out = new FileOutputStream(f) ; // 文件输出流
- oos = new ObjectOutputStream(out) ;
- oos.writeObject(new Person("张三",30)) ; // 保存对象
- oos.close() ; // 关闭
- }
- public static void dser() throws Exception {
- File f = new File("D:" + File.separator + "test.txt") ; // 定义保存路径
- ObjectInputStream ois = null ; // 声明对象输入流
- InputStream input = new FileInputStream(f) ; // 文件输入流
- ois = new ObjectInputStream(input) ; // 实例化对象输入流
- Object obj = ois.readObject() ; // 读取对象
- ois.close() ; // 关闭
- System.out.println(obj) ;
- }
- };
5、序列化一组对象
如果要保存多个对象,则最好使用对象数组的形式完成。
- import java.io.File ;
- import java.io.IOException ;
- import java.io.FileOutputStream ;
- import java.io.OutputStream ;
- import java.io.ObjectOutputStream ;
- import java.io.FileInputStream ;
- import java.io.InputStream ;
- import java.io.ObjectInputStream ;
- public class SerDemo05{
- public static void main(String args[]) throws Exception{
- Person per[] = {new Person("张三",30),new Person("李四",31),
- new Person("王五",32)} ;
- ser(per) ;
- Object o[] = (Object[])dser() ;
- for(int i=0;i<o.length;i++){
- Person p = (Person)o[i] ;
- System.out.println(p) ;
- }
- }
- public static void ser(Object obj[]) throws Exception {
- File f = new File("D:" + File.separator + "test.txt") ; // 定义保存路径
- ObjectOutputStream oos = null ; // 声明对象输出流
- OutputStream out = new FileOutputStream(f) ; // 文件输出流
- oos = new ObjectOutputStream(out) ;
- oos.writeObject(obj) ; // 保存对象
- oos.close() ; // 关闭
- }
- public static Object[] dser() throws Exception {
- File f = new File("D:" + File.separator + "test.txt") ; // 定义保存路径
- ObjectInputStream ois = null ; // 声明对象输入流
- InputStream input = new FileInputStream(f) ; // 文件输入流
- ois = new ObjectInputStream(input) ; // 实例化对象输入流
- Object obj[] = (Object[])ois.readObject() ; // 读取对象
- ois.close() ; // 关闭
- return obj ;
- }
- };
总结:
1、对象序列化的作用,对象序列化并不一定都向文件中保存,也有可能面向于其他的输入或输出2、被序列化的对象的类必须实现Serializable 接口,如果某个属性不希望被保存下来,则可以使用transient 关键字声明。3、ObjectOutputStream 序列化对象,ObjectInputStream 反序列化对象4、Externalizable 接口作用: 开发人员手式实现序列化的操作5、使用序列化保存一组对象的时候要使用对象数组的形式操作