Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 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 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...