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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 被有实名认证的闲鱼卖家骗了怎么办 我收到了方正的提示函怎么办 淘宝刷q币单被骗了怎么办 中通快递已签收但是东西丢了怎么办 手机不版本低不支持微信下载怎么办 淘宝虚拟商品不支持7天退货怎么办 卖虚拟物品遇到恶意退款买家怎么办 淘宝极速退款后卖家拒绝退款怎么办 我的天猫积分不让换券了怎么办 微信手机话费充错了怎么办 自己进货在淘宝卖被投诉假货怎么办 京东买的电器售后后服务差怎么办 京东到家申请退款卖家不处理怎么办 天猫买了假货商品下架了怎么办 淘宝本地生活服务不能入驻了怎么办 淘宝店铺名在电脑上搜索不到怎么办 已经将退货寄回店家硬说没有怎么办 微信申诉账号短信验证失败怎么办 京东账号换手机号收不到短信怎么办 我的手机收不到短信通知怎么办? 淘宝卖家发货物流单号写错了怎么办 商铺买东西不给调换大小怎么办 圆通快递物流信息一直没更新怎么办 中通快递三天没更新物流信息怎么办 快递已经到了物流信息不更新怎么办 天天快递查询不更新物流信息怎么办 买车下个月分期全部付清怎么办手续 天猫客服介入以后商家不退款怎么办 淘宝上买代购奢侈品买到假货怎么办 淘宝退货卖家收到货拒绝退款怎么办 没收到货但申请了退货退款怎么办 小米商城预约中德手机没货怎么办 电脑用百度网盘下载速度超慢怎么办 ios网盘下载速度太慢怎么办 小米手机4x卡机了怎么办 小米手机4x屏幕点不动了怎么办 苹果手机连接u盘没反应怎么办 苹果官网储蓄卡分期额度不够怎么办 京东买东西发票信息填写错了怎么办 华为v9手机激活密码忘了怎么办 公司报销发票纸质的丢了怎么办