Externalizable&&Serializable

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

你可能感兴趣的:(序列化对象)