我目前正在写以下方法:
- 从内存中读取对象
- 将对象写入内存
我试图保存一个 String 对象并再次读取它,但我的代码不起作用。
这是我的代码:
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();
}
}