Data storage and access in Android (1) --- in the form of Files
Data storage and access in Android (1) --- 1.1 files are stored in the mobile phone memory in the form of files: // *** store data to the/data/package name/files/jxn.txt file String data = "test "; /// data/package name/filesFile filesDir = context. getFilesDir (); File file = new File (filesDir, "jxn.txt"); FileOutputStream fos = new FileOutputStream (file); fos. write (data. getBytes (); fos. flush (); fos. close (); // *** read data from the/data/package name/files/jxn.txt File String data = "test"; File filesDir = context. GetFilesDir (); File file = new File (filesDir, "jxn.txt"); FileInputStream FD = new FileInputStream (file); BufferedReader reader = new BufferedReader (new InputStreamReader (FS )); string text = reader. readLine (); reader. close (); supplement: 1. In the Android context, the openFileOutput () method can write data to 2 in the/data/package name/files directory. In the Android context, openFileInput () the method can be used to read file 3 under the/data/package name/files directory. The specific implementation process is the same as saving data to files in the J2SE environment. The first parameter of the eg: // openFileOutput () method is used to specify the file name. It cannot contain the path separator "/". If the file does not exist, Android will automatically create the // storage path: /data/package name/files/jxn.txt FileOutputStream outStream = context. openFileOutput ("jxn.txt", Context. MODE_PRIVATE); SharedPreferences1, Android provides a SharedPreferences class, which is a lightweight storage class, especially suitable for saving software configuration parameters. 2. Use SharedPreferences to save the data. The xml file is used to store the data. The file is stored in the/data/package name/shared_prefs directory. 3. context. the first parameter of the getSharedPreferences (name, mode) method is used to specify the name of the file. The name does not need a suffix. Android will automatically add it. xml suffix. // *** Store data to/data/package name/shared_prefs/jxn. in the xml file, // data/package name/shared_prefs/jxn. xmlSharedPreferences sp = context. getSharedPreferences ("jxn", Context. MODE_PRIVATE); // obtain an Editor object Editor edit = sp. edit (); // save data edit. putString ("number", number); edit. putString ("password", password); // submit, and the data is saved to the file for editing. commit (); // *** from/data/package name/shared_prefs/jxn. read data in the xml file SharedPreferences sp = context. getSha RedPreferences ("jxn", Context. MODE_PRIVATE); String number = sp. getString ("number", null); String password = sp. getString ("password", null); 1.2 store the file in the SD card: // *** store the data to the/mnt/sdcard/jxn.txt file // determine whether the current mobile phone has an SD card. If the mobile phone has an SD card and can be read/written, the method returns Environment. MEDIA_MOUNTEDString state = Environment. getExternalStorageState (); if (! Environment. MEDIA_MOUNTED.equals (state) {return false;} String data = "test"; // It is generally/mnt/sdcard, but the directory of the SD card of different mobile phones may be different File sdCardFile = Environment. getExternalStorageDirectory (); File file = new File (sdCardFile, "jxn.txt"); FileOutputStream fos = new FileOutputStream (file); fos. write (data. getBytes (); fos. flush (); fos. close (); // *** read data from the/mnt/sdcard/jxn.txt file // determine whether the current mobile phone has an SD card String state = Environment. getEx TernalStorageState (); if (! Environment. MEDIA_MOUNTED.equals (state) {return null;} File sdCardFile = Environment. getExternalStorageDirectory (); File file = new File (sdCardFile, "jxn.txt"); BufferedReader br = new BufferedReader (new InputStreamReader (new FileInputStream (file); String text = br. readLine (); br. close ();