Develop > API Guides >数据存储
https://developer.android.com/guide/topics/data/data-storage.html#pref
https://www.youtube.com/watch?v=HWKcVeNcs20&index=11&list=PLHOqLxXumAI-ASmOAbuV9oD-gxHvjsjUU
文件的写入位置
System.out: /data/data/com.example.e560.m1110a/shared_prefs/xml.xml
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<boolean name="chk" value="true" />
<string name="key">new value</string>
<set name="StringSet">
<string>String1</string>
<string>String2</string>
</set>
<int name="int" value="123" />
</map>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setinfo();
addinfo();
getinfo();
File f = new File("/data/data/com.example.e560.m1110a/shared_prefs");
System.out.println("----------");
File[] fs = f.listFiles();
System.out.println(fs.length);
for (int i = 0; i < fs.length; i++) {
File temp = fs[i];
System.out.println(temp.getPath());
}
}
protected void getinfo() {
SharedPreferences sp = this.getSharedPreferences("xml", MODE_PRIVATE);
LinearLayout lv = new LinearLayout(this);
EditText et = new EditText(this);
et.setText(sp.getAll().toString());
lv.addView(et);
setContentView(lv);
}
protected void setinfo() {
SharedPreferences sp = this.getSharedPreferences("xml", MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.putString("key", "value");
edit.putInt("int", 123);
edit.putBoolean("chk", true);
edit.commit();
}
protected void addinfo() {
SharedPreferences sp = this.getSharedPreferences("xml", MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
edit.putString("key", "new value");
Set<String> list = new HashSet<String>();
list.add("String1");
list.add("String2");
edit.putStringSet("StringSet", list);
edit.commit();
}
}