1

In an android app I need to persist some data to a file that is in an ArrayList of objects of type A. The objects of type A in ArrayList consist of standard primitive objects, except each A object also contains an ArrayList of objects of type B.

The B objects are also composed of primitive data types. Neither class A nor B has any particular code for implementing Serializable at this point.

My question is how should I go about writing this data to a file. Do I need to add specific support to serialize these objects? I suspect not since they only contain primitive data types, but OTOH I suspect I need to since ArrayList is not a primitive type itself.

Guidance is much appreciated. I want to do the simplest thing possible. I know I could write my own JSONSerializer code that manually serializes each field in class A, and invokes JSON serializer code from class B to serialize the ArrayList items, and stuff it all in a file. I also am not interested in running this all through SQLLite.

WHile I have found many posts on serializing arraylists I have found none for nested ArrayLists. Thanks in advance for the assist.

2 Answers 2

2

If you want to use Serializable, you should have no problems, because ArrayList is Serializable. Just make sure your non primitive objects are Serializable too.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! All I had to do was make object A serializable, then take the classes whose objects were within arraylists within object A and make their classes serializable and it all worked. What was confusing as a Java newbie is that I didn't realize that "making a class serializable" only means adding the "implements serializable" syntax to the class declaration, and adding the required versionID to the class. I was under the impression I had to write some code to do the serializing itself as well. Thanks very much!
0

Use ObjectInputStream and ObjectOutputStream, something like:

public static void saveArrayListToFile(ArrayList<Book> books, String filePath)
{
    ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(filePath)));
    oos.writeObject(books);
    oos.close();
}

// ...

public static ArrayList<Book> loadArrayListFromFile(String filePath)
{
    ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(filePath)));
    try
    {
        return (ArrayList<Book>) ois.readObject();
    }
    finally
    {
        ois.close();
    }
}

Note that Book class must implements Serializable interface. I did not test the code, but it should work. You may need to try-catch for any exceptions in the code above.

1 Comment

Thank you. Yes making Book seializable worked. In my situation though Book had within it another ArrayList<Chapters> for example, so I had to add the serializable attribute to class Chapters as well. BTW your code stub did have an issue for me on the ois.readObject call I had to suppress the "Type safety: Unchecked cast from Object to ArrayList<Book>" warning from the compiler - however the code works fine and dandy. I would vote you guys up but my rep needs to go up before I can vote!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.