Search code examples
javaandroidfilestore

Storing complex object to a file in android application


FileOutputStream fout = context.getApplicationContext()
                    .openFileOutput(FILENAME, Context.MODE_PRIVATE);
ObjectOutputStream out = new ObjectOutputStream(fout);
out.writeObject(complexObject);

Will this code work for a complexObject which is an instance of a complex class. By complex I mean that it might contain several arraylists of instances of other classes, many instance variables?


Solution

  • Yes, ObjectOutputStream can serialize a complex tree of objects as long as all objects in this tree implement Serializable. It also serializes all java primitive types.