包含多个对象的 ArrayList 的序列化,不保存对象状态 [英] Serialization of ArrayList containing multiple objects, doesn't save object state

查看:20
本文介绍了包含多个对象的 ArrayList 的序列化,不保存对象状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

对象序列化(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 类的一部分:

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

推荐答案

是的,它也对我有用检查

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

}

输出:object1

这篇关于包含多个对象的 ArrayList 的序列化,不保存对象状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆