0

I need to write/read an ArrayList<Marker> to a file. Here is what I did so far:

Save List:

private void saveToFile(ArrayList<Marker> arrayList) {
        try {
            FileOutputStream fileOutputStream = openFileOutput("test.txt", Context.MODE_PRIVATE);
            ObjectOutputStream out = new ObjectOutputStream(fileOutputStream);
            out.writeObject(arrayList);
            out.close();
            fileOutputStream.close();

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

        }
    }

Load List

private ArrayList<Marker> loadFromFile() {
        ArrayList<Marker> savedArrayList = null;

        try {
            FileInputStream inputStream = openFileInput("test.txt");
            ObjectInputStream in = new ObjectInputStream(inputStream);
            savedArrayList = (ArrayList<Marker>) in.readObject();
            in.close();
            inputStream.close();

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }

        return savedArrayList;
    }

When I run the code I'm getting ClassNotFoundException. The issue is that I need to store the whole ArrayList<Marker> as object. I have found a solution that it separates the Marker into lat/long and write it to the text file as numbers, but it is not suitable for me. I understand that the Marker Class is not Serializable, but how then it can be saved as an whole object?

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
Seiran
  • 126
  • 9

0 Answers0