本文介绍了如何序列化/反序列化的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
这篇关于如何序列化/反序列化的ArrayList(对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!