我们要序列化某个类,其实并不用将这个类 implements Serializable,可以借助writeObject()readObject() 方法来对某个类进行序列化。

例如下面这个例子:我们要将 SerializableTest 这个类进行序列化。

package com.example.dongyayun.androidartsearch.Serializable;

/**
 * Created by dongyayun on 16/1/16.
 */
public class SerializableTest {
    private int t1;
    private int t2;
    private transient int t3;
    private String s1;


    public int getT3() {
        return t3;
    }

    public void setT3(int t3) {
        this.t3 = t3;
    }

    public int getT1() {
        return t1;
    }

    public void setT1(int t1) {
        this.t1 = t1;
    }

    public int getT2() {
        return t2;
    }

    public void setT2(int t2) {
        this.t2 = t2;
    }

    public String getS1() {
        return s1;
    }

    public void setS1(String s1) {
        this.s1 = s1;
    }

    @Override
    public String toString() {
        return super.toString() + " getS1 = " + getS1() + " getT1 = " + getT1() + " getT2 = " + getT2() + " getT3() =  " + getT3();
    }


}

我们可以这么做:

package com.example.dongyayun.androidartsearch.Serializable;

import java.io.IOException;
import java.io.Serializable;

/**
 * Created by dongyayun on 16/1/15.
 */
public class MySerializable implements Serializable {
    static final long serialVersionUID = 6201933920870624595L; //很重要

    SerializableTest mySerializable = null;

    public MySerializable(SerializableTest s) {
        mySerializable = s;
    }

    public SerializableTest getMySerializable() {
        return mySerializable;
    }


    public void setS1(String s1) {
        mySerializable.setS1(s1);
    }

    public void setT1(int t1) {
        mySerializable.setT1(t1);
    }

    public void setT2(int t2) {
        mySerializable.setT2(t2);
    }

    public void setT3(int t3) {
        mySerializable.setT3(t3);
    }

    private void writeObject(java.io.ObjectOutputStream out)
            throws IOException {
        // write 'this' to 'out'...
        out.writeInt(mySerializable.getT1());
        out.writeInt(mySerializable.getT2());
        out.writeInt(mySerializable.getT3());
        out.writeObject(mySerializable.getS1());
    }

    private void readObject(java.io.ObjectInputStream in)
            throws IOException, ClassNotFoundException {
        // populate the fields of 'this' from the data in 'in'...
        mySerializable = new SerializableTest();
        mySerializable.setT1(in.readInt());
        mySerializable.setT2(in.readInt());
        mySerializable.setT3(in.readInt());
        mySerializable.setS1((String) in.readObject());
    }
}

测试程序:

private void testMySerializable() {
      MySerializable mySerializable = new MySerializable(new SerializableTest());
      mySerializable.setS1("a");
      mySerializable.setT1(1);
      mySerializable.setT2(2);
      mySerializable.setT3(3);
      Log.d("MainActivity", "before " + mySerializable.getMySerializable().toString());
      MySerializable mySerializable1 = null;

      try {
          FileOutputStream stream = new FileOutputStream(new File(getFilesDir(), "a.txt"), false);

          ObjectOutputStream objectOutputStream = new ObjectOutputStream(stream);

          objectOutputStream.writeObject(mySerializable);


          FileInputStream stream1 = new FileInputStream(new File(getFilesDir(), "a.txt"));
          ObjectInputStream inputStream = new ObjectInputStream(stream1);

          mySerializable1 = (MySerializable) inputStream.readObject();

          Log.d("MainActivity", "after " + mySerializable1.getMySerializable().toString());
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      } catch (ClassNotFoundException e) {
          e.printStackTrace();
      }

  }

运行结果:

01-16 05:22:28.531  before com.example.dongyayun.androidartsearch.Serializable.SerializableTest@27843bd getS1 = a getT1 = 1 getT2 = 2 getT3() =  3
01-16 05:22:28.537  after com.example.dongyayun.androidartsearch.Serializable.SerializableTest@500310a getS1 = a getT1 = 1 getT2 = 2 getT3() =  3

在不破坏SerializableTest 类的情况下,我们用MySerializable 来对SerializableTest进行一层封装,并且在实现writeObject() 时仅对我们所需要的数据进行序列化保存,同样,从java 多态性来看,MySerializable同样适合SerializableTest的子类,所以尽情的发挥吧~~

好了,Serializable 就说到这吧