Externalizable&&Serializable
来源:互联网 发布:虚拟局域网软件排行 编辑:程序博客网 时间:2024/05/22 14:45
java中序列化对象有两种方法,实现Serializable和实现Externalizable。其中Externalizable继承了Serializable。下面通过一个例子来讲解一下这两种方式的实现并对他们的效率进行比较。
1. 开发中的通常写法
package com.example.GuChuanhang2;import java.io.*;/** - Created by Chuanhang.Gu on 2016/5/2. */public class Student implements Serializable { public static final long serialVersionUID = 1; protected String identityId; protected String name; protected int age; protected String readingSchool;public Student(String identityId,String name,int age,String school) { this.identityId = identityId; this.name = name; this.age = age; this.readingSchool = school;} public Student(){ } public String getIdentityId(){ return this.identityId; }}
注意事项:
- serialVersionUID 这个参数一定要加上,数据结构调整后,修改该字段,就不能读取“旧版”数据,避免由于字段缺失导致的各种崩溃。
- 这种方式一定要添加一个默认的无参构造方法,否则读取数据失败
2. 实现Externalizable
package com.example.GuChuanhang2;import java.io.*;/** - Created by Chuanhang.Gu on 2016/5/2. */public class EfficientStudent implements Externalizable { public static final long serialVersionUID = 1; protected String identityId; protected String name; protected int age; protected String readingSchool; public EfficientStudent(String identityId, String name, int age, String school) { this.identityId = identityId; this.name = name; this.age = age; this.readingSchool = school; } public EfficientStudent(){ } @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { identityId = in.readUTF(); name = in.readUTF(); age = in.readInt(); readingSchool = in.readUTF(); } @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeUTF(identityId); out.writeUTF(name ); out.writeInt(age); out.writeUTF(readingSchool); } public String getIdentityId(){ return this.identityId; }}
注意事项:
- 同样要填写serialVersionUID,用于数据结构的调整
- 必须实现writeExternal方法&&readExternal方法,这个与Android中的Parcelable接口十分类似。
- 同样要有一个默认的无参构造方法。
3.测试Serializable工具类:
package com.example.GuChuanhang2;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.util.ArrayList;public class Main2 { public static void main(String[] args) { Student student=null; try { writeOne(); student= (Student)readObject("money_foo"); } catch (Exception e) { e.printStackTrace(); } System.out.println("xxxxxxxxxxxxxxxxxxxx"+student.getIdentityId()); } private static void writeOne( ) { try { System.out.println("Writing one instance"); Student student = new Student("123","name",23,"jianda"); writeObject("money_foo", student); } catch(Exception e){ e.printStackTrace(); } } private static void writeObject(String filename, Object object) throws Exception { FileOutputStream fileOutputStream = new FileOutputStream(filename); ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); long startTime = System.currentTimeMillis( ); objectOutputStream.writeObject(object); objectOutputStream.flush( ); objectOutputStream.close( ); System.out.println("write Time: " + (System.currentTimeMillis( ) - startTime)); } private static Object readObject(String filename) throws Exception { FileInputStream fileInputStream = new FileInputStream(filename); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); long startTime = System.currentTimeMillis( ); Object object= objectInputStream.readObject(); objectInputStream.close( ); System.out.println("read Time: " + (System.currentTimeMillis( ) - startTime)); return object; }}
运行5次,平均写入时间35ms,读取时间3ms
3.测试Externalizable工具类:
package com.example.GuChuanhang2;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.util.ArrayList;public class Main { public static void main(String[] args) { EfficientStudent student=null; try { writeOne(); student= (EfficientStudent)readObject("money_foo"); } catch (Exception e) { e.printStackTrace(); } System.out.println("xxxxxxxxxxxxxxxxxxxx"+student.getIdentityId()); } private static void writeOne( ) { try { System.out.println("Writing one instance"); EfficientStudent student = new EfficientStudent("123","name",23,"jianda"); writeObject("money_foo", student); } catch(Exception e){ e.printStackTrace(); } } private static void writeObject(String filename, Object object) throws Exception { FileOutputStream fileOutputStream = new FileOutputStream(filename); ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); long startTime = System.currentTimeMillis( ); objectOutputStream.writeObject(object); objectOutputStream.flush( ); objectOutputStream.close( ); System.out.println("write Time: " + (System.currentTimeMillis( ) - startTime)); } private static Object readObject(String filename) throws Exception { FileInputStream fileInputStream = new FileInputStream(filename); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); long startTime = System.currentTimeMillis( ); Object object= objectInputStream.readObject(); objectInputStream.close( ); System.out.println("read Time: " + (System.currentTimeMillis( ) - startTime)); return object; }}
运行5次,平均写入时间15ms,读取时间2ms。
参考地址:
http://www.onjava.com/pub/a/onjava/excerpt/JavaRMI_10/?page=6
1 0
- Externalizable&&Serializable
- Serializable与Externalizable
- Serializable vs Externalizable
- Serializable和Externalizable
- Serializable 和 Externalizable
- Serializable,externalizable,comprable
- Serializable、Externalizable、Parcelable实现
- Serializable和Externalizable
- Externalizable和Serializable
- Serializable和Externalizable
- Serializable和Externalizable
- Serializable和Externalizable浅析
- 序列化:Serializable和Externalizable
- 序列化:Serializable、Externalizable、Parcelable
- 序列化接口:Serializable/Externalizable
- 序列化Serializable和Externalizable
- Serializable Externalizable transient singleton readRsolve()
- 序列化--Serializable接口和Externalizable接口
- Window8与Windows8.1休眠文件破解和加密磁盘解密
- HDOJ1008
- PCB设计小问题
- 获得经纬度通过函数传到后台处理
- 二叉树的所有路径,因为有些IDE不支持itoa方法,自定义的int->stringstream->string,
- Externalizable&&Serializable
- 【读书笔记】【Android 开发艺术探索】第 2 章 IPC 机制
- Excel Sheet Column Number
- 电路杂记
- linux文件合并,去重
- 【BZOJ3653】谈笑风生【主席树】【DFS序】
- hdu 5672(尺取法)
- 十二、图的遍历--(3)广度优先搜索算法
- EMC小资料