关键代码:
/* ================================== 读取值 ================================== */ // 取得活动的Preferences对象(当前activity this,一般是私有MODE_PRIVATE) SharedPreferences settings = getPreferences(Activity.MODE_PRIVATE); // 取得值,默认false value = settings.getBoolean("key", false); /* ================================== 保存值 ================================== */ // 取得活动的Preferences对象 SharedPreferences uiState = getPreferences(Activity.MODE_PRIVATE); // 取得编辑对象 SharedPreferences.Editor editor = uiState.edit(); // 添加值 editor.putBoolean("key", value); // 提交保存(只有提交才能保存) editor.commit();生成的文件放在:/data/data/XXX/shared_prefs/...
/* =================================== 读取值 ================================== */ // 构建Properties对象 Properties properties = new Properties(); // 打开输入文件 FileInputStream stream = this.openFileInput("inputfile"); // 读取文件内容 properties.load(stream); /* =================================== 保存值 ================================== */ // 构建Properties对象 Properties properties = new Properties(); // 将数据打包成Properties properties.put("key", "value"); // 打开输出文件 FileOutputStream stream = this.openFileOutput("outputfile", mode); // 将打包好的数据写入文件 properties.store(stream, "");
Android存储数据的几种方式--Shared preference和files
原文:http://blog.csdn.net/zeb_perfect/article/details/44651805