我有一个ArrayList<ItemList>
如何序列化/反序列化的ArrayList(对象)
其中ITEMLIST是:
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() {
}
}
我尝试序列这份名单:
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();
}
I thi这是工作,因为我发现这个文件在文件夹中。
但我无法从文件反序列化到ArrayList<ItemList>
代码:
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<ItemList>
? 我总是发现IOException。
请发布整个异常堆栈跟踪 – Tomer 2012-07-09 15:46:25
您是否仅序列化ArrayList,即“it”变量或类ItemList? – prashant 2012-07-09 15:51:09
'我总是发现IOException'。是的,但你读过它包含的信息吗?它包含答案。 – EJP 2012-07-11 23:54:16