At first, make your object serializable and let the IDE (in my case Eclipse) generate a serialVersionUID for you:
1 2 3 4 5 6 7 8 9 |
public class Post implements Serializable{ private static final long serialVersionUID = 7884946435027239199L; public String firstname; public String lastname; } |
Writing to files with serialization and writing objects to file:
1 2 3 4 |
FileOutputStream fileOutputStream = openFileOutput("personobject", Context.MODE_PRIVATE); ObjectOutputStream objectOutputStream= new ObjectOutputStream(fileOutputStream ); objectOutputStream.writeObject(yourObject); objectOutputStream.close(); |
Reading files with serialization and get it as object:
1 2 3 |
FileInputStream fileInputStream = openFileInput("personobject"); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); Person person = (Person)objectInputStream.readObject(); |
Thats it!