【JAVA IO】_对象序列化笔记
来源:互联网 发布:安卓编程软件手机软件 编辑:程序博客网 时间:2024/06/15 15:09
转载自http://blog.csdn.net/e421083458/article/details/8562245
【JAVA IO】_对象序列化笔记
本章目标:
掌握对象序列化的作用
掌握Serializable接口作用
可以使用ObjectOutputStream进行对象序列化操作
可以使用ObjectinputStream进行对象反序列化操作
掌握Externalizable接口的作用及与Serializable接口的实现区别
掌握transient关键字的作用
可以序列化一组对象
3.1、什么叫对象序列化
一个对象产生后实际上是在内存中为其开辟了一个存储空间,方便存储信息。
对象序列化,就是把一个对象变为二进制的数据流的一种方法,通过对象序列化可以方便的实现对象的传输或存储.
如果一个类的对象想被序列化,则对象所在的类必须实现java.io.Serializable接口。此接口定义如下:
public interface Serializable{}
一个类不能平白无故的被序列化。
但是,在此接口中没有任何一个方法,此接口属于一个标识接口,表示具备了某种能力。
例如:现在定义一个类,此类可以被序列化。
- import java.io.Serializable;
- public class Person implements Serializable{
- private String name;
- private int age;
- public Person(String name,int age){
- this.name = name;
- this.age = age;
- }
- public String toString(){
- return "姓名:"+this.name+";年龄:"+this.age;
- }
- }
import java.io.Serializable;public class Person implements Serializable{ private String name; private int age; public Person(String name,int age){ this.name = name; this.age = age; } public String toString(){ return "姓名:"+this.name+";年龄:"+this.age; }}
以后此类的对象,就可以被序列化了,变为二进制byte流。
对象的序列化和反序列化
要想完成对象的输入或输出,还必须依靠对象输出流(ObjectOutputStream)和对象输入流(ObjectInputStream)
使用对象输出流输出序列化对象的步骤,有时也称之为序列化,而使用对象输入流读入对象的过程,有时也称为反序列化。
serialVersionUID
对象的序列化和反序列化要考虑JDK版本不统一的问题,所以在序列化操作中引入了serialVersionUID的常量,可以通过此常量来验证版本的一致性,在进行反序列化时,JVM会把传来的字节流中的serialVersionUID与本地的相应实体类的serialVersionUID进行比较,如果相同就认为是一致的,可以进行反序列化,否则就会出现序列化版本不一致的异常。
- import java.io.Serializable;
- public class Person implements Serializable{
- private static final long serialVersionUID = 1L;
- private String name;
- private int age;
- public Person(String name,int age){
- this.name = name;
- this.age = age;
- }
- public String toString(){
- return "姓名:"+this.name+";年龄:"+this.age;
- }
- }
import java.io.Serializable;public class Person implements Serializable{ private static final long serialVersionUID = 1L; private String name; private int age; public Person(String name,int age){ this.name = name; this.age = age; } public String toString(){ return "姓名:"+this.name+";年龄:"+this.age; }}
如果使用开发工具开发,就有编写此代码,则会出现一些安全警告的信息。
3.2、对象的序列化及反序列化操作
对象序列化依靠ObjectOutputStream,对象反序列化依靠ObjectInputStream
3.2.1、序列化:ObjectOutputStream
定义格式:
public class ObjectOutputStream extends OutputStream implements ObjectOutput,ObjectStreamConstants
- 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() ; // 关闭
- }
- };
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() ; // 关闭 }};
到底序列化了哪些东西呢?
所有的对象拥有各自的属性值,但是所有的方法都是公共的,所以序列化对象的时候实际上序列化的就是属性。
3.2.2、反序列化:ObjectInputStream
定义格式
public class ObjectInputStream extends InputStream implements ObjectInput,ObjectStreamConstants
- 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) ;
- }
- };
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.3、Externalizable接口
使用Serializable接口可以方便的序列化一个对象,但是在序列化操作中也提供了另外一种序列化机制————Exnternalizable接口
定义格式:
public interface Externalizable extends Serializable{
public void writeExternal(ObjectOutput out)throws IOException;
public void readExternal(ObjectInput in)throws IOException,ClassNotFoundException;
}
在使用Exterlizable接口的时候需要在被序列化的类中定义一个无参构造。
- import java.io.Externalizable;
- import java.io.*;
- public class Person implements Externalizable{
- private static final long serialVersionUID = 1L;
- private String name;
- private int age;
- public Person(){}
- public Person(String name,int age){
- this.name = name;
- this.age = age;
- }
- public String toString(){
- return "姓名:"+this.name+";年龄:"+this.age;
- }
- public void writeExternal(ObjectOutput out)throws IOException{
- out.writeObject(this.name);
- out.writeInt(this.age);
- }
- public void readExternal(ObjectInput in)throws IOException,ClassNotFoundException{
- this.name = (String)in.readObject();
- this.age = in.readInt();
- }
- }
import java.io.Externalizable;import java.io.*;public class Person implements Externalizable{ private static final long serialVersionUID = 1L; private String name; private int age; public Person(){} public Person(String name,int age){ this.name = name; this.age = age; } public String toString(){ return "姓名:"+this.name+";年龄:"+this.age; } public void writeExternal(ObjectOutput out)throws IOException{ out.writeObject(this.name); out.writeInt(this.age); } public void readExternal(ObjectInput in)throws IOException,ClassNotFoundException{ this.name = (String)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) ;
- }
- };
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) ; }};
3.4、transient关键字
当使用Serializable接口实现序列化操作时,如果一个对象中的某个属性不希望被序列化的话,则可以使用transient关键字进行声明。
- import java.io.Serializable;
- public class Person implements Serializable{
- private static final long serialVersionUID = 1L;
- private transient String name;
- private int age;
- public Person(String name,int age){
- this.name = name;
- this.age = age;
- }
- public String toString(){
- return "姓名:"+this.name+";年龄:"+this.age;
- }
- }
import java.io.Serializable;public class Person implements Serializable{ private static final long serialVersionUID = 1L; private transient String name; private int age; public Person(String name,int age){ this.name = name; this.age = age; } public String 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) ;
- }
- };
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) ; }};
transient+Serializable接口完全可以取代Externalizable接口的功能。
3.5、序列化一组对象(就是将Obj由单个对象变为多个对象)
- 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 ;
- }
- };
- 【JAVA IO】_对象序列化笔记
- 【JAVA IO】_对象序列化笔记
- 【JAVA IO】_对象序列化笔记
- Java IO _对象序列化
- Java IO _对象序列化
- Java IO _对象序列化
- java.io 与对象序列化 笔记
- Java IO流(对象序列化)
- Java IO流与对象IO序列化
- Java IO流与对象IO序列化
- Java IO流与对象IO序列化(转)
- Java学习笔记之IO(十三):对象的序列化和反序列化
- Java笔记5 IO<4>对象的序列化、管道流
- 12. JAVA IO Part 4 (对象的序列化) ----- 学习笔记
- 【JAVA IO】_回退流笔记
- 【JAVA IO】_回退流笔记
- Java中对象的序列化(实现java.io.Serializable)
- [Java] IO-05 ObjectIO (java 对象序列化)
- Oracle Security API - FND_FUCTION.TEST
- 不同字序下位域定义内存存放规则
- Vi 特殊操作
- Vi Myself常用命令
- mysql常用命令(转)
- 【JAVA IO】_对象序列化笔记
- Android 缓存Bitmaps
- 安装oracle 10g时遇到时间不同步的问题prkc-1002,如下图
- Vim 编辑多个文件
- Intel处理器手册系统编程指南第2章(3)
- Q:NSString length负数比较问题
- hadoop对于压缩文件的支持及算法优缺点
- pexpect.pxssh.ExceptionPxssh: Could not establish connection to host
- 面向对象(二)