首先要做的事:Read this 了解您有哪些选择。
现在回答您关于如何将数据保存在本地来自here 的一些代码的问题:
String FILENAME = "hello_file";
String yourJSON = downloadedJSON.toString() // depends what kind of lib you are using
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(yourJSON.getBytes());
fos.close();
将 FILENAME 存储在 SharedPreferences 中,以便您稍后阅读它以防它发生变化,或者只是为了了解如何在 SharedPreferences 中存储数据:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); // PREFS_NAME should be a static final String property
SharedPreferences.Editor editor = settings.edit();
editor.putString("myFileName", FILENAME).commit();
然后当您稍后重新打开应用程序时,您可以这样做:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String FILENAME = setting.getString("myFileName", "");
if (!FILENAME.isEmpty()) {
readFile(FILENAME);
}
最后从here:读回文件为String
FileInputStream fis;
fis = openFileInput("test.txt");
StringBuffer fileContent = new StringBuffer("");
byte[] buffer = new byte[1024];
while ((n = fis.read(buffer)) != -1) {
fileContent.append(new String(buffer, 0, n));
}
在使用此代码之前,请尝试理解每个代码块。如果您遇到困难,请浏览 Android 文档并使用 Google。 Plenty of tutorials out there。数据存储是基础,不仅适用于 Android!