繁体   English   中英

包含多个对象的ArrayList的序列化,不保存对象状态

[英]Serialization of ArrayList containing multiple objects, doesn't save object state

我似乎无法弄清楚为什么序列化会保存和恢复对象列表 ,而不是它们的状态。 将显示列表,但不显示对象中包含的标题。 对象类实现Serializable。

对象的序列化(“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 ();
    }
}

反序列化:

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

将对象添加到适配器:

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

编辑:TestObject类的一部分:

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;
}

是的,它也适合我检查

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;
    }

}

输出:object1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM