如何序列化/反序列化的ArrayList(对象)

如何序列化/反序列化的ArrayList(对象)

问题描述:

我有一个的ArrayList< ITEMLIST>

在这里ITEMLIST是:

where ItemList is:

public class ItemList {
    public ArrayList<Item> it = new ArrayList<Item>();
    public String name = "";

    public ItemList() {
    }
}

和项目是:

public class Item {
    public String name = "";
    public int count = 0;

    public Item() {
    }
}

我尝试序列此列表:

I try to serialize this list:

try {
            FileOutputStream fileOut = new FileOutputStream(sdDir + serFile);
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(List_Of_Lists);
            out.close();
            fileOut.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

我觉得这是工作,becouse我觉得在这个文件夹中的文件。

I think it's work, becouse I find this file in folder.

但我不能从文件反序列化到的ArrayList&LT; ITEMLIST&GT;

But I can't deserialize from file to ArrayList<ItemList>

code:

        try {
            FileInputStream fileIn = new FileInputStream(sdDir + serFile);
            ObjectInputStream in = new ObjectInputStream(fileIn);
            List_Of_Lists = (ArrayList<ItemList>) in.readObject(); 
            Log.i("palval", "dir.exists()");
            in.close();
            fileIn.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

我如何反序列化这个的ArrayList&LT; ITEMLIST&GT; ? 我总是赶IOException异常。

How I can deserialize this ArrayList<ItemList>? I always catch IOException.

项目 ITEMLIST 类需要实现Serializable