How do I serialize an object and save it to a file in Android?
Saving (w/o exception handling code):
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);ObjectOutputStream os = new ObjectOutputStream(fos);os.writeObject(this);os.close();fos.close();
Loading (w/o exception handling code):
FileInputStream fis = context.openFileInput(fileName);ObjectInputStream is = new ObjectInputStream(fis);SimpleClass simpleClass = (SimpleClass) is.readObject();is.close();fis.close();
I've tried this 2 options (read/write), with plain objects, array of objects (150 objects), Map:
Option1:
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);ObjectOutputStream os = new ObjectOutputStream(fos);os.writeObject(this);os.close();
Option2:
SharedPreferences mPrefs=app.getSharedPreferences(app.getApplicationInfo().name, Context.MODE_PRIVATE);SharedPreferences.Editor ed=mPrefs.edit();Gson gson = new Gson(); ed.putString("myObjectKey", gson.toJson(objectToSave));ed.commit();
Option 2 is twice quicker than option 1
The option 2 inconvenience is that you have to make specific code for read:
Gson gson = new Gson();JsonParser parser=new JsonParser();//object arr exampleJsonArray arr=parser.parse(mPrefs.getString("myArrKey", null)).getAsJsonArray();events=new Event[arr.size()];int i=0;for (JsonElement jsonElement : arr) events[i++]=gson.fromJson(jsonElement, Event.class);//Object examplepagination=gson.fromJson(parser.parse(jsonPagination).getAsJsonObject(), Pagination.class);
I just made a class to handle this with Generics, so it can be used with all the object types that are serializable:
public class SerializableManager { /** * Saves a serializable object. * * @param context The application context. * @param objectToSave The object to save. * @param fileName The name of the file. * @param <T> The type of the object. */ public static <T extends Serializable> void saveSerializable(Context context, T objectToSave, String fileName) { try { FileOutputStream fileOutputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE); ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); objectOutputStream.writeObject(objectToSave); objectOutputStream.close(); fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } /** * Loads a serializable object. * * @param context The application context. * @param fileName The filename. * @param <T> The object type. * * @return the serializable object. */ public static<T extends Serializable> T readSerializable(Context context, String fileName) { T objectToReturn = null; try { FileInputStream fileInputStream = context.openFileInput(fileName); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); objectToReturn = (T) objectInputStream.readObject(); objectInputStream.close(); fileInputStream.close(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } return objectToReturn; } /** * Removes a specified file. * * @param context The application context. * @param filename The name of the file. */ public static void removeSerializable(Context context, String filename) { context.deleteFile(filename); }}