ObjectInputStream gives me an empty object (but the read file is not empty)

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

+4
source share
3 answers

Your arraylist is static. Static variables are associated with a class, not an object. Therefore, they are not serialized.

If you want to serialize static variables, you need to override readObject and writeObject.

Here is additional information - http://java.sun.com/developer/technicalArticles/Programming/serialization/

The best solution here is to revise the data structure and make the emergency worker unstable if he maintains the state of the facility.

EDIT: alternatively, you can only serialize the arraialist (as @Ted suggests below)

+6
source

The problem is that variables marked as static will not be serialized, except that you implement

 private void readObject(ObjectInputStream ois){} 

and

 private void writeObject(ObjectOutputStream oos){} 

ObjectOutputStream - JavaDoc:

The default serialization mechanism for an object records the class of the object, the class signature, and the values ​​of all non-transient and non-static fields.

+3
source

Since the only data stored in SaveState is a static array, you will get more success by simply serializing and deserializing the array:

 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.favoriteBusStopNumbers); out.close(); } catch (Exception e) {e.printStackTrace();} } @SuppressWarnings("unchecked") public static void loadData(){ ObjectInput in; try { File inFile = new File(Environment.getExternalStorageDirectory(), DIRECTORY_NAME+"appSaveState.data"); in = new ObjectInputStream(new FileInputStream(inFile)); SaveState.favoriteBusStopNumbers=(ArrayList<String>) in.readObject(); in.close(); } catch (Exception e) {e.printStackTrace();} } 

(You need to suppress the casting warning on a generic type.)

You really don't need the SaveState.instance field.

+1
source

All Articles