Android的数据存储
SharedPreferences
在一个Activity中存入数据
public class SharedPreferenceActivity extends Activity { private EditText editText = null; private CheckBox checkBox = null; private Button button = null; private String ed_sString = null; private String ch_sString = null; private SharedPreferences.Editor editor = null; private SharedPreferences sharedPreferences = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sharepreference); editText = (EditText) findViewById(R.id.sh_edit); checkBox = (CheckBox) findViewById(R.id.sh_checkBox1); button = (Button) findViewById(R.id.sh_button1); sharedPreferences = getSharedPreferences("test", Activity.MODE_APPEND); editor = sharedPreferences.edit(); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (editText.getText() != null) { ed_sString = editText.getText().toString(); } if (checkBox.isChecked()) { ch_sString = checkBox.toString(); } System.out.println(ed_sString + "---" + checkBox.isChecked()); editor.putString("name", ed_sString); editor.putBoolean("checked", checkBox.isChecked()); editor.commit(); } }); } }
在另一个Activity中取出数据
public class SharedPreferencetgetActivity extends Activity { TextView tView_1 = null; TextView tView_2 = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.sharepreferece); tView_1 = (TextView) findViewById(R.id.share_1); tView_2 = (TextView) findViewById(R.id.share_2); SharedPreferences sharedPreferences = getSharedPreferences("test", Activity.MODE_PRIVATE); Boolean k = sharedPreferences.getBoolean("checked", false); tView_1.setText(sharedPreferences.getString("name", "122")); tView_2.setText(k.toString()); } }
setting
使用PreferenceFragment制作Setting:
preferences.XML文件
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <PreferenceCategory android:key="Category_1_key" android:summary="Category_1_sum" android:title="Category_1_title" > <EditTextPreference android:key="edit_1_key" android:summary="edit_1_sum" android:title="edit_1_title" > </EditTextPreference> <CheckBoxPreference android:selectable="true" android:title="checkbox_1_title" > </CheckBoxPreference> <ListPreference android:dialogTitle="list" android:entries="@array/pre" android:entryValues="@array/pre" android:key="list_key" android:summary="list_sum" android:title="list_title" /> </PreferenceCategory> <Preference android:title="other" > </Preference> <PreferenceScreen android:title="Screen_SET" > <intent android:action="android.settings.WIFI_SETTINGS" /> <EditTextPreference android:key="edittext_1_set" android:title="edittext_1_title" > </EditTextPreference> </PreferenceScreen> </PreferenceScreen>
SharePreferenceStrActivity.java 文件
public class SharePreferenceStrActivity extends Activity { @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); getFragmentManager().beginTransaction() .replace(android.R.id.content, new PrefsFragement()).commit(); } @SuppressLint("NewApi") public static class PrefsFragement extends PreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preference); } } }
FileOutputStream与FlieInputStream
FileOutputStream
public class FileOperate extends Activity { private FileOutputStream fileOutputStream = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fileoperateinto); try { fileOutputStream = openFileOutput("test", Activity.MODE_APPEND); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } PrintStream out = new PrintStream(fileOutputStream); out.print("姓名"); } }
FileInputStream
public class FileOperateRead extends Activity { FileInputStream fileInputStream = null; String stringBuffer = null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.fileoperateinto); try { fileInputStream = openFileInput("test"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } Scanner scanner = new Scanner(fileInputStream); while (scanner.hasNext()) { System.out.println(scanner.next()); } } }
IO文件流的操作(可做缓存操作)
存入
public class FileIOOperate extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fileoperateinto); if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { System.out.println(Environment.getExternalStorageState().toString() + File.separator); System.out.println(Environment.getExternalStorageDirectory().toString() + File.separator); File file = new File(Environment.getExternalStorageDirectory() .toString() + File.separator + "cjm" + File.separator + "test2"); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } PrintStream out = null; try { out = new PrintStream(new FileOutputStream(file, true)); out.println("testmytest"); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (out != null) { out.close(); } } } else { Toast.makeText(FileIOOperate.this, "没有SD", Toast.LENGTH_SHORT) .show(); } } }
取出
public class FileIOOperateGet extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.fileoperateinto); if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { System.out.println(Environment.getExternalStorageState().toString() + File.separator); File file = new File(Environment.getExternalStorageDirectory() .toString() + File.separator + "cjm" + File.separator + "test2"); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } Scanner scanner = null; try { scanner = new Scanner(new FileInputStream(file)); while (scanner.hasNext()) { System.out.println(scanner.next()); } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (scanner != null) { scanner.close(); } } } else { Toast.makeText(FileIOOperateGet.this, "没有SD", Toast.LENGTH_SHORT) .show(); } } }
操作raw中的文件
public class OperatedRaw extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.fileoperateinto); Resources resources = getResources(); InputStream inputStream = resources.openRawResource(R.raw.cjm); Scanner scanner = new Scanner(inputStream); while (scanner.hasNext()) { System.out.println(scanner.next()); } scanner.close(); try { inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
该文件为只读文件
此处写json来操作数据的方式,因为在此后比较常用
public class JSONOperateArray extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); // 建立jsonObject对象用来装所有的数据 JSONObject jsonObject = new JSONObject(); // 建立jsonarray数组用来装每组数据 JSONArray jsonArray = new JSONArray(); for (int x = 0; x < 4; x++) { // 一组数据装在一个temp中 JSONObject temp = new JSONObject(); try { temp.put("name", "temp" + x); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } jsonArray.put(temp); } // 装入整个所有的数据 jsonObject 就是我们用来传送的数据 try { jsonObject.put("jsonAll", jsonArray); } catch (JSONException e) { e.printStackTrace(); } // 建立SDcard的文件夹和文件来装json数据 if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { System.out.println(Environment.getExternalStorageState().toString() + File.separator); System.out.println(Environment.getExternalStorageDirectory() .toString() + File.separator); File file = new File(Environment.getExternalStorageDirectory() .toString() + File.separator + "cjm" + File.separator + "json"); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } // 通过Print将数据存在外部扩展卡地文件中 PrintStream out = null; try { out = new PrintStream(new FileOutputStream(file, true)); System.out.println(jsonArray.toString()); out.println(jsonArray.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (out != null) { out.close(); } } } else { Toast.makeText(JSONOperateArray.this, "没有SDCard", Toast.LENGTH_SHORT) .show(); } } }
复杂数据存入
public class JSONOperateArray extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); // 建立jsonObject对象用来装所有的数据 JSONObject jsonObject = new JSONObject(); // 建立jsonarray数组用来装每组数据 JSONArray jsonArray = new JSONArray(); for (int x = 0; x < 4; x++) { // 一组数据装在一个temp中 JSONObject temp = new JSONObject(); try { //在temp中加入更多的数据 temp.put("name", "temp" + x); temp.put("address", "temp" + x); temp.put("email", "temp" + x); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } jsonArray.put(temp); } // 装入整个所有的数据 jsonObject 就是我们用来传送的数据 try { //在jsonObject中加入更多的数据 jsonObject.put("json1", jsonArray); jsonObject.put("json2", "jsonObject"+1); jsonObject.put("json3", "jsonObject"+2); } catch (JSONException e) { e.printStackTrace(); } // 建立SDcard的文件夹和文件来装json数据 if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { System.out.println(Environment.getExternalStorageState().toString() + File.separator); System.out.println(Environment.getExternalStorageDirectory() .toString() + File.separator); File file = new File(Environment.getExternalStorageDirectory() .toString() + File.separator + "cjm" + File.separator + "json"); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } // 通过Print将数据存在外部扩展卡地文件中 PrintStream out = null; try { out = new PrintStream(new FileOutputStream(file, true)); System.out.println(jsonObject.toString()); out.println(jsonObject.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (out != null) { out.close(); } } } else { Toast.makeText(JSONOperateArray.this, "没有SDCard", Toast.LENGTH_SHORT).show(); } } }
获取json数据简单的
public class JSONOperateGET extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.fileoperateinto); if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { System.out.println(Environment.getExternalStorageState().toString() + File.separator); System.out.println(Environment.getExternalStorageDirectory() .toString() + File.separator); File file = new File(Environment.getExternalStorageDirectory() .toString() + File.separator + "cjm" + File.separator + "json1"); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } Scanner scanner = null; try { scanner = new Scanner(new FileInputStream(file)); while (scanner.hasNext()) { String reString = scanner.next(); StringBuffer stringBuffer = new StringBuffer(); List<Map<String, Object>> all = getJson(reString); for (int k = 0; k < all.size(); k++) { Map<String, Object> map = all.get(k); System.out.println("1111--" + map.get("111")); System.out.println("2222--" + map.get("222")); System.out.println("3333--" + map.get("333")); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (scanner != null) { scanner.close(); } } } else { Toast.makeText(JSONOperateGET.this, "没有SD", Toast.LENGTH_SHORT) .show(); } } private List<Map<String, Object>> getJson(String s) throws Exception { s = "[" + s + "]"; System.out.println("list---" + s); List<Map<String, Object>> all = new ArrayList<Map<String, Object>>(); JSONArray jsonArray = new JSONArray(s); for (int i = 0; i < jsonArray.length(); i++) { Map<String, Object> map = new HashMap<String, Object>(); JSONObject jsonObject = jsonArray.getJSONObject(i); map.put("111", jsonObject.getString("json2")); map.put("222", jsonObject.getString("json3")); map.put("333", jsonObject.getString("json1")); all.add(map); } return all; } }

浙公网安备 33010602011771号