安卓本地存储

1.SharedPreferences类将数据保存在一个xml文件中,文件存放在/data/data/<package name>/shared_prefs目录下

// //创建异或获取一个已经存在的sharedPreferences对象
SharedPreferences sharedPreferences = getSharedPreferences("mySP", Context.MODE_PRIVATE);


2. 新增或编辑值

Editor editor = sharedPreferences.edit();
editor.putString("username", "ccc");
editor.putString("password", "ddd");
editor.commit();


 

3. 读取值,第二个参数为默认值,如果取不到,则返回这个默认值

String username = sharedPreferences.getString("username", "");
String password = sharedPreferences.getString("password", "");


 4. 删除值

Editor editor = sharedPreferences.edit();
editor.remove("password");
editor.commit();


 

你可能感兴趣的:(xml,String,存储)