如何序列化/反序列化arrayList(Object)

Val*_*Val 14 java serialization android deserialization

我有一个 ArrayList<ItemList>

其中ItemList是:

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

    public ItemList() {
    }
}
Run Code Online (Sandbox Code Playgroud)

和项目是:

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

    public Item() {
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试序列化这个列表:

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();
        }
Run Code Online (Sandbox Code Playgroud)

我认为这是有效的,因为我在文件夹中找到了这个文件.

但我无法从文件反序列化 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();
        }
Run Code Online (Sandbox Code Playgroud)

我怎么能反序化ArrayList<ItemList>呢?我总是捕获IOException.

Psh*_*emo 13

ItemItemList班级需要implements Serializable