I’m currently writing to methods to:
- read object from memory
- write object to memory
I tried to save a String object and read it again, yet my code didn’t work.
This is my code:
public void writeObjectToMemory(String filename, Object object) {
FileOutputStream fos;
try {
fos = game.openFileOutput(filename, Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(object);
os.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
public void readObjectFromMemory(String filename, Object object) {
FileInputStream fis;
try {
fis = game.openFileInput(filename);
ObjectInputStream is = new ObjectInputStream(fis);
object = is.readObject();
is.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (StreamCorruptedException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
You wrote :
But
objectis only a copy of the argument you passed to the function. You can change that copy inside the method (as you do), but this will have no effect on the actual parameter.You have to return your object instead :
Read this for more details : http://www.cs.utoronto.ca/~dianeh/tutorials/params/