我正在开发Android应用程序,我需要保存和加载功能.
我希望使用此功能将重要数据保存在私有应用程序存储中:
public static void save(Object file, String fileName, Context context)
throws IOException, FileNotFoundException {
FileOutputStream fos = context.openFileOutput(fileName,
context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(file);
oos.flush();
oos.close();
fos.close();
System.out.println(TAG + ": File saved successful!");
}
Run Code Online (Sandbox Code Playgroud)
我还写了加载数据的方法.这是有效的,因为我知道文件的确切名称.
我现在需要动态保存文件,动态生成名称.如何列出保存在pricate app存储中的文件?所以我可以加载一个?
干得好!
File dir = getFilesDir();
File[] subFiles = dir.listFiles();
if (subFiles != null)
{
for (File file : subFiles)
{
// Here is each file
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4330 次 |
| 最近记录: |