Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to serialize/deserialize arrayList(Object)

I have an ArrayList<ItemList>

where ItemList is:

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

    public ItemList() {
    }
}

and Item is:

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

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

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

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

like image 679
Val Avatar asked Jul 09 '12 15:07

Val


1 Answers

Your Item and ItemList classes needs to implements Serializable

like image 68
Pshemo Avatar answered Oct 05 '22 00:10

Pshemo