外部写入时外部化接口的行为具有比读取外部更多的属性

时间:2017-10-29 09:20:50

标签: java serialization externalizable

我正在探索可外化的界面。当我在序列化文件中写入比我读的更多属性时,我发现了一个奇怪的行为。以下是我的员工班。

public class Employee implements Externalizable {

    private int id;
    private String name;
    private Boolean isTempEmp;      

    public Employee(int id, String name, Boolean isTempEmp) {
        super();
        this.id = id;
        this.name = name;
        this.isTempEmp = isTempEmp;
    }

    @Override
    public String toString() {
        return "Employee [id=" + id + ", name=" + name + ", isTempEmp=" + isTempEmp + "]";
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {

        out.writeInt(id);
        out.writeUTF(name);
        out.writeBoolean(isTempEmp);
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {

        this.id=in.readInt();
        this.name=in.readUTF();
        //this.isTempEmp=in.readBoolean();
    }

}

以下是我在文件中外部化员工对象并从文件中读取的类

public class ExternalizeTest2 {

    public static void main(String[] args) {

        Employee emp1=new Employee(1, "Swet", true);
        Employee emp4=new Employee(2, "Varun", true);
        try(OutputStream os=new FileOutputStream("Employee.txt");
                ObjectOutputStream oos=new ObjectOutputStream(os);) {

            emp1.writeExternal(oos);
            emp4.writeExternal(oos);


        }catch (Exception e) {
            e.printStackTrace();
        }

        Employee emp2=new Employee(3, "Test1", false);
        Employee emp3=new Employee(4, "Test2", false);
        try(InputStream is=new FileInputStream("Employee.txt");
                ObjectInputStream ois=new ObjectInputStream(is);) {
            emp2.readExternal(ois);
            emp3.readExternal(ois);


        }catch (Exception e) {
            e.printStackTrace();

        }

        System.out.println("After externalization: \n"+emp2.toString());
        System.out.println(emp3.toString());
    }

}

以下是我得到的输出:

java.io.EOFException
    at java.io.ObjectInputStream$BlockDataInputStream.readByte(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.readUTFChar(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.readUTFBody(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.readUTF(Unknown Source)
    at java.io.ObjectInputStream.readUTF(Unknown Source)
    at serialization.externalize.Employee.readExternal(Employee.java:40)
    at serialization.externalize.ExternalizeTest2.main(ExternalizeTest2.java:32)


    After externalization: 
    Employee [id=1, name=Swet, isTempEmp=false]
    Employee [id=16777216, name=Test2, isTempEmp=false]

不确定分配给emp3的id是16777216。

1 个答案:

答案 0 :(得分:0)

emp1.writeExternal(oos);
emp4.writeExternal(oos);

你做错了。这应该是:

oos.writeObject(emp1);
oos.writeObject(emp4);

类似地

emp2.readExternal(ois);
emp3.readExternal(ois);

应该是

emp2 = (Employee)ois.readObject();
emp3 = (Employee)ois.readObject();

你拥有它的方式,你只是在调用你自己的,错误的方法:根本不做序列化。