【发布时间】:2016-02-22 16:00:53
【问题描述】:
我正在尝试将数据从应用程序保存到内部存储并重新加载,但我觉得我错过了一些东西并且在加载中找不到文件。
保存代码:
private void saveScanData(List<MyStack> surf) throws IOException {
Log.i(TAG, "Saving");
String filename = String.format("Scan%05d.data", scanNumber);
scanNumber += 1;
ObjectOutput out;
File outFile = new File(Environment.getExternalStorageDirectory(), filename);
out = new ObjectOutputStream(new FileOutputStream(outFile));
out.writeObject(surf);
out.close();
}
加载代码:
public void load(View view) {
ObjectInput in;
List<MyStack> surf= null;
try {
in = new ObjectInputStream(new FileInputStream("Scan%05d.data"));
surf= (List<MyStack>) in.readObject();
in.close();
} catch (Exception e) {e.printStackTrace();}
Model model= new Model(surf);
Intent intent = new Intent(this, EditorPresenter.class);
intent.putExtra("model", model);
startActivity(intent);
}
感谢您的帮助。
【问题讨论】:
-
为了避免任何其他因素,您是否尝试过立即调用 saveScanData 然后加载以查看会发生什么?如果可行,那么很可能是何时(或是否)调用它的问题。
-
保存和加载在两个不同的活动中,但我仍然尝试在应用程序的同一会话中执行此操作(保存、返回主要活动并尝试加载),但我收到了这个错误。
标签: java android save load internal-storage