问题描述:

I just tried to read back an object I wrote to a file. I have two functions for it.

public static Serializable_test deSerialize_object(String filename)

{

File a = new File(filename);

ObjectInputStream in = null;

try{

in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(a)));

}catch(IOException e){

e.printStackTrace();

}

Serializable_test obj1 = null;

try{

obj1 = (Serializable_test) in.readObject();

in.close();

}catch(IOException e){

e.printStackTrace();

}catch(ClassNotFoundException e){

e.printStackTrace();

}

return obj1;

}

public static void deSerialize_object(Serializable_test obj,String filename){

File a = new File(filename);

//int objcount = 0;

ObjectInputStream in = null;

try{

in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(a)));

}catch(IOException e){

e.printStackTrace();

}

try{

obj = (Serializable_test) in.readObject();

in.close();

}catch(EOFException e){

System.out.println("END of object files reached");

}catch(IOException e){

e.printStackTrace();

}catch(ClassNotFoundException e){

e.printStackTrace();

}

}

The first method works fine... the second method is supposed to assign the read object to the passed reference but it does not work at all (the state of the passed object remains the same as the initialized state whereas its supposed to be that of the read object) is there something wrong in the code?.

网友答案:

This is because in Java references are passed by value. Your second method receives a copy of the reference to your object and hence updating that reference in the second method doesn't affect the original object.

The simplest solution would be to return the object read from the serialized stream. If you really want to update an existing object with the de-serialized object, provide an mutator update method which sets the fields of the passed in object to that of the newly read object.

public static void deSerialize_object(Serializable_test obj, String filename) {
    File a = new File(filename);     
    ObjectInputStream in = null;
    try {
        ObjectInputStream in =
            new ObjectInputStream(new BufferedInputStream(new FileInputStream(a)));
        Serializable_test newObj = (Serializable_test) in.readObject();
        obj.update(newObject);
    } catch(Exception e) {
        e.printStackTrace();
    } finally {
        if (in != null) in.close();
    }
}

Also make sure you close your resources in the finally block.

网友答案:

The line

obj =  (Serializable_test) in.readObject();

Creates a new object and puts a reference to that object in the obj variable.

You effectively overwrite the reference you passed to the method.

In these case I let the object update themselves using a "copyFrom" method or a "readFromStream" method. But I prefer the function based approach.

网友答案:

In Java, references to objects are passed by value.

A workaround would be something like

// obj must be an initialized Serializable_test[1] before you call

public static void deSerialize_object(Serializable_test[] obj,String filename){
    ...
    obj[0] = (Serializable_test) in.readObject();
    ...
}

But I don't see why you don't want to just return the reference.

相关阅读:
Top