Serializing the object
suggest changeGood old Java object serialization is available to you in Android. You can define Serializable classes like:
class Circle implements Serializable {
final int radius;
final String name;
Circle(int radius, String name) {
this.radius = radius;
this.name = name;
}
}
and then write them to the ObjectOutputStream:
File myFile = new File(getFilesDir(), "myObjects.bin");
FileOutputStream out = new FileOutputStream(myFile);
ObjectOutputStream oout = new ObjectOutputStream(new BufferedOutputStream(out));
oout.writeObject(new Circle(10, "One"));
oout.writeObject(new Circle(12, "Two"));
oout.close();
Java object serialization may be either a perfect or a really bad choice, depending on what you want to do with it. That choice is outside the scope of this tutorial and sometimes a matter of opinion. The streams are closed only if both writes succeed. Read about the versioning first if you decide to use it.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents