Question

I receive a byte[] from my internal storage and now I don't know how to convert it into my ArrayList.

I'm referring to this post. -->>THIS<<--

snipped code:

                ArrayList<KFZInfo> toReturn = null;
                FileInputStream fis;
                try {
                    fis = openFileInput("kfzList");
                    ObjectInputStream oi = new ObjectInputStream(fis);
                    toReturn = (ArrayList<KFZInfo>) oi.readObject();
                    oi.close();
                } catch (FileNotFoundException e) {
                    System.out.println(e);
                } catch (IOException e) {
                    System.out.println(e);
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

At toReturn = (ArrayList<KFZInfo>) oi.readObject(); it is throwing an error which says: java.lang.ClassCastException: byte[] cannot be cast to java.utio.ArrayList

And thats how I write it on the internal storage:

try {
                    FileOutputStream fos = openFileOutput("kfzList", Context.MODE_PRIVATE);
                    ObjectOutputStream oo = new ObjectOutputStream(fos);
                    oo.writeObject(listKfzInfo.toString().getBytes());
                    oo.flush();
                    oo.close();
                    fos.close();

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

}

How do I solve this problem?

Was it helpful?

Solution

Change

oo.writeObject(listKfzInfo.toString().getBytes());

with

oo.writeObject(listKfzInfo);

I assuming that listKfzInfo is an ArrayList<KFZInfo> and that KFZInfo implements Serializable and that all fields inside KFZInfo implements Serializable

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top