I have this serializable class that I use to store strings in an ArrayList binary.
public class SaveState implements Serializable{ public static ArrayList <String> favoriteBusStopNumbers = new ArrayList<String>(); public static SaveState instance=new SaveState(); }
I use this method to store an instance with an arrayList of rows after this array is filled with data that I have to store:
public static void saveData(){ ObjectOutput out; try { //primero comprobamos si existe el directorio, y si no, lo creamos. File folder = new File(Environment.getExternalStorageDirectory() + DIRECTORY_NAME); if(!folder.exists()) folder.mkdirs(); File outFile = new File(Environment.getExternalStorageDirectory(), DIRECTORY_NAME+"appSaveState.data"); out = new ObjectOutputStream(new FileOutputStream(outFile)); out.writeObject(SaveState.instance); out.close(); } catch (Exception e) {e.printStackTrace();} }
And finally, I use this method in the init of my application to upload a file and populate my SaveState.instance variable with previously saved data:
public static void loadData(){ ObjectInput in; try { File inFile = new File(Environment.getExternalStorageDirectory(), DIRECTORY_NAME+"appSaveState.data"); in = new ObjectInputStream(new FileInputStream(inFile)); SaveState.instance=(SaveState) in.readObject(); in.close(); } catch (Exception e) {e.printStackTrace();} }
When I save the data, the file is created correctly with the data filling in the object, I know it because the file has more than 0 Kbytes of disk space. But something is wrong here, because when I launch my application and load the data, my SaveState.instance variable gets an empty ArrayList of lines ....... then ¿what is wrong in the code?
thanks
source share