现在我试图用这个
FileOutputStream fos = getContext().openFileOutput("CalEvents", Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(returnlist);
oos.close();
Run Code Online (Sandbox Code Playgroud)
为了将"返回列表"这是一个ArrayList保存到文件"CalEvents"现在我的问题是,这是正确的方法吗?以及如何检索列表?
提前致谢
这是你想要做的吗?
FileInputStream fis;
try {
fis = openFileInput("CalEvents");
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList<Object> returnlist = (ArrayList<Object>) ois.readObject();
ois.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
编辑:可以简化:
FileInputStream fis;
try {
fis = openFileInput("CalEvents");
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList<Object> returnlist = (ArrayList<Object>) ois.readObject();
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
假设你在一个扩展的类Context(如Activity).如果没有,那么你将不得不openFileInput()在扩展的对象上调用该方法Context.
| 归档时间: |
|
| 查看次数: |
16171 次 |
| 最近记录: |