Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

java - Serialization of ArrayList containing multiple objects, doesn't save object state

I can't seem to figure out why serialization saves and restores the list of objects, but not the their state. The list is displayed, but not the title which is contained in the object. The object class implements Serializable.

Serialization of objects ("c"):

arrayList.add ( c );
    String fileName = "testFile";

    try {
        FileOutputStream fos = this.openFileOutput ( fileName, Context.MODE_PRIVATE );
        ObjectOutputStream os = new ObjectOutputStream ( fos );
        os.writeObject ( arrayList );
        fos.close ();
        os.close ();
    } catch ( Exception ex ) {
        ex.printStackTrace ();
    }
}

Deserialization:

    FileInputStream fis = this.openFileInput ( fileName );
        ObjectInputStream ois = new ObjectInputStream ( fis );
        arrayList = ( ArrayList<TestObject> ) ois.readObject ();
        ois.close ();
        return arrayList;

Adding objects to adapter:

    for ( TestObject c : arrayList ) {
        adapter.add ( c );
    }

Edit: part of the TestObject class:

public class TestObject implements Serializable {

private String mName;

@Override
public String toString () {
    return mName;
}

public String getName () {
    return mName;
}

public void setName ( String name ) {
    mName = name;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

yes, It is also working for me check

public class SerializationIssue {
private static final String fileName = "testFile";

public static void main(String[] args) {
    TestObject object1= new TestObject();
    TestObject object2=new TestObject();
    object1.setName("object1");
    object2.setName("object2");

    List<TestObject> list=new ArrayList<TestObject>();
    list.add(object1);
    list.add(object2);

    serializeList(list);
    ArrayList<TestObject> deserializedList=desializeDemo();
    System.out.println(deserializedList.get(0).getName());

}

private static ArrayList desializeDemo() {
    ArrayList<TestObject> deserializedList;
     try
      {
         FileInputStream fileIn = new FileInputStream(fileName);
         ObjectInputStream in = new ObjectInputStream(fileIn);
         deserializedList= (ArrayList<TestObject>) in.readObject();
         in.close();
         fileIn.close();
      }catch(IOException i)
      {
         i.printStackTrace();
         return null;
      }catch(ClassNotFoundException c)
      {
         System.out.println("Employee class not found");
         c.printStackTrace();
         return null;
      }
    return deserializedList;
}

private static void serializeList(List<TestObject> list) {
    // TODO Auto-generated method stub

        try {
            FileOutputStream fos = new FileOutputStream(fileName);
            ObjectOutputStream os = new ObjectOutputStream ( fos );
            os.writeObject ( list );
            fos.close ();
            os.close ();
        } catch ( Exception ex ) {
            ex.printStackTrace ();
        }

}
}

TestObject bean

public class TestObject implements Serializable{

    /**
     * serial version.
     */
    private static final long serialVersionUID = 1L;
    String name;
    public TestObject(String name) {
        super();
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

Output:object1


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...