Android Serializing the object


Example

The old good Java object serialization is available for you in Android. you can define Serializable classes like:

  class Cirle implements Serializable {
    final int radius;
    final String name;

    Circle(int radius, int name) {
      this.radius = radius;
      this.name = name;
    }
  }

and then write then 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 perfect or really bad choice, depending on what do you want to do with it - outside the scope of this tutorial and sometimes opinion based. Read about the versioning first if you decide to use it.