0

I've been trying to save and load an object, but it isn't working. When I tell it to save, I have this:

public void doSave(View v) {
    ObjectOutputStream out;
    try {
        FileOutputStream outFile = new FileOutputStream("testThingy321.ser");
        out = new ObjectOutputStream(outFile);
        out.writeObject(hero);
        out.close();
        outFile.close();
    } catch (Exception e) {e.printStackTrace();}
}

And when I tell it to load, I have this:

try {
            FileInputStream fileIn = new FileInputStream("testThingy321.ser");
            ObjectInput in = new ObjectInputStream(fileIn);       
            hero = (Protag) in.readObject();
            in.close();
            fileIn.close();
        } catch (Exception e) {e.printStackTrace();}

I don't have a good enough understanding of how this works to tell why it isn't working, and it's pretty much what the samples I've found have told me to say. However, it never seems to even do anything. Can someone tell me what I would have to do to let it save or load?

Edit: I checked the logcat and I'm getting this:

05-17 22:25:46.098: W/System.err(24774): java.io.FileNotFoundException: testThingy321.ser: open failed: EROFS (Read-only file system)

aattss
  • 49
  • 4
  • 1
    Check your logcat, most likely there are error being printed out and you are not seeing them – Joel May 18 '16 at 01:35

1 Answers1

1

Your Protag class should implement Serializable interface and define readObject and writeObject methods as described here Serialization - readObject writeObject overrides

Community
  • 1
  • 1
  • Try this in order to locate your file String filePath = context.getFilesDir().getPath().toString() + "/"testThingy321.ser")"; FileOutputStream outFile = new FileOutputStream(new File(filePath); – Atef Haouari May 18 '16 at 02:58