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

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

问题描述

我似乎无法找出原因系列化,保存并恢复列表中的的对象,而不是他们的状态。列表被显示出来,但不被包含在对象的标题。对象类实现Serializable接口。

对象的序列化(C):

  arrayList.add(C);
    字符串文件名=TESTFILE;

    尝试 {
        FileOutputStream中FOS = this.openFileOutput(文件名,Context.MODE_PRIVATE);
        ObjectOutputStream的OS =新的ObjectOutputStream(FOS);
        os.writeObject(数组列表);
        fos.close();
        os.close();
    }赶上(例外前){
        ex.printStackTrace();
    }
}
 

反序列化:

 的FileInputStream FIS = this.openFileInput(文件名);
        ObjectInputStream的OIS =新的ObjectInputStream(FIS);
        ArrayList的=(ArrayList的<的TestObject>)ois.readObject();
        ois.close();
        返回数组列表;
 

添加对象适配器:

 的(的TestObject C:ArrayList的){
        adapter.add(C);
    }
 

编辑:在的TestObject类的一部分:

 公共类的TestObject实现Serializable {

私人字符串MNAME;

@覆盖
公共字符串的toString(){
    返回MNAME;
}

公共字符串的getName(){
    返回MNAME;
}

公共无效setname可以(字符串名称){
    MNAME =名称;
}
 

解决方案

是的,这也为我工作 检查

 公共类SerializationIssue {
私有静态最后字符串文件名=TESTFILE;

公共静态无效的主要(字串[] args){
    TestObject中object1 =新的TestObject();
    的TestObject = Object2的新的TestObject();
    object1.setName(object1);
    object2.setName(Object2的);

    名单<的TestObject>名单=新的ArrayList<的TestObject>();
    list.add(object1);
    list.add(Object2的);

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

}

私有静态的ArrayList desializeDemo(){
    ArrayList的<的TestObject> deserializedList;
     尝试
      {
         的FileInputStream FILEIN =新的FileInputStream(文件名);
         ObjectInputStream中的=新的ObjectInputStream(FILEIN);
         deserializedList =(ArrayList的<的TestObject>)in.readObject();
         附寄();
         fileIn.close();
      }赶上(IOException异常I)
      {
         i.printStackTrace();
         返回null;
      }赶上(ClassNotFoundException的三)
      {
         的System.out.println(Employee类未找到);
         c.printStackTrace();
         返回null;
      }
    返回deserializedList;
}

私有静态无效serializeList(名单<的TestObject>列表){
    // TODO自动生成方法存根

        尝试 {
            FileOutputStream中FOS =新的FileOutputStream(文件名);
            ObjectOutputStream的OS =新的ObjectOutputStream(FOS);
            os.writeObject(名单);
            fos.close();
            os.close();
        }赶上(例外前){
            ex.printStackTrace();
        }

}
}
 

输出:object1

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

解决方案

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

}
}

Output:object1

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

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