如何在java序列化中获得瞬态值

时间:2014-06-17 02:57:59

标签: java serialization

我遇到一个问题,我无法获得瞬态对象变量的值,该变量本身包含另一个对象引用。 Serializable Dog类具有名为Not serializable Collar的瞬态值。 Collar本身具有非可序列化的对象引用。如何在序列化和反序列化中获得Collar值。请解决这个问题。

Dog.java

package main;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Dog implements Serializable{
    private static final long serialVersionUID = 1L;

    private String name;
    private transient Collar collar;

    public Dog(String name, Collar collar) {
        this.name = name;
        this.collar = collar;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Collar getCollar() {
        return collar;
    }

    public void setCollar(Collar collar) {
        this.collar = collar;
    }

    private void writeObject(ObjectOutputStream os) {
        try {
            os.defaultWriteObject();
            os.writeUTF(collar.getName());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void readObject(ObjectInputStream is) {
        try {
            is.defaultReadObject();
            collar = new Collar(is.readUTF(), null);  // here, need a collar.getCobbar()
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    @Override
    public String toString() {
        return "dog name : " + this.name + "   collar : " + this.collar;
    }
}

Collar.java

package main;

public class Collar {
    private String name;
    private Cobbar cobbar;

    public Collar(String name, Cobbar cobbar) {
        this.name = name;
        this.cobbar = cobbar;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "collar name : " + this.name + " cobbar : " + cobbar;
    }

}

Cobbar.java

package main;

public class Cobbar {
    private String name;

    public Cobbar(String name) {
        this.name  = name;
    }

    @Override
    public String toString() {
        return "cobbar name : " + name;
    }

}

MyClass.java

package main;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class MyClass {

    public static void main(String[] args) {
        Cobbar cobbar = new Cobbar("Cobbar object");
        Collar collar = new Collar("Collar object", cobbar);
        Dog dog = new Dog("Dog object", collar);


        try {
            FileOutputStream fis = new FileOutputStream("D:\\SCJPsimple.txt");
            ObjectOutputStream os = new ObjectOutputStream(fis);
            os.writeObject(dog);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            FileInputStream fis = new FileInputStream("D:\\SCJPsimple.txt");
            ObjectInputStream os = new ObjectInputStream(fis);
            dog = (Dog) os.readObject();

            System.out.println("Dog str : " + dog.toString());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

}

2 个答案:

答案 0 :(得分:0)

将自定义writeObject()和readObject()方法添加到Dog或Collar,或使字段非瞬态,或添加'serializableFields'成员。

答案 1 :(得分:0)

transient关键字表示“不要序列化”,因此制作Dog课程Serializable的简单答案就是“删除transient关键字”。

完整的答案是制作嵌套类Serializable

public class Dog implements Serializable {
    private String name;
    private Collar collar;

    // rest of class omitted
}

public class Collar implements Serializable {
    private String name;
    private Cobbar cobbar;
    // rest of class omitted
}

public class Cobbar implements Serializable {
    private String name;
    // rest of class omitted
}

JDK中的大多数类都是Serializable,因此您的String字段等都可以。