First, sharedpreferences
Files in. xml format are stored in the data/data/package/shared_prefs/directory.
Storage steps:
1. Create file: getsharedpreferences (name, mode); name is the file name, and if there is a file that exists, it is no longer created and is used directly with this document.
2, get the file editor: Sharedpreferences.editor Se=preferences.edit ();
3, Deposit Data: se.putstring ("Et_content", Et.gettext (). toString ()), call the editor's Putxxx method, can be stored in different types of data, a total of putint (key,
Value), Putlong (key, value), Putfloat (key, value), Putboolean (key, value), putstring (key, value), and Putstringset (arg0, ARG1) Six Parties
Method.
4, Submit changes: Se.commit ();
For example: Getsharedpreferences ("Dcy", Context.mode_private). Edit (). Putstring ("username", Et_username.gettext (). toString (). Trim
()). commit ();
Fetch data: getsharedpreferences ("Gxx", Context.mode_private). GetString ("Et_content", "");
Second, internal storage (stored in the phone internal storage space)
Storage Directory is data/data/package/files/
Storage steps:
1. Open storage File: Openfileoutput (name, mode); name does not restrict type, can be specified by itself
2, Write Data: fos.write (buffer);
3. Refresh Close stream: Fos.flush (); Fos.close ();
For example:
FileOutputStream fos=openfileoutput ("Dcy.txt", context.mode_private);
Fos.write (Text.gettext (). toString (). GetBytes ());
Fos.flush ();
Fos.close ();
Remove the data: Openfileinput ("Dcy.txt"); only the file name of the read data needs to be specified.
Getfilesdir (); Gets an absolute path/data/data/com.example.studentlogin/files
Getcachedir (); Gets an absolute path/data/data/com.example.studentlogin/cache
Third, SDcard storage
Storing data on the phone's sdcard requires declaring an externally stored writable permission: <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/ >
If you want to determine if sdcard is mounted, you also need a permission: <uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
Stored procedures:
1. Get sdcard Current status: Environment.getexternalstoragestate (); Returns a string constant, such as environment.media_mounted (SDcard mounted)
2. Determine if SDcard is mounted: Environment.MEDIA_MOUNTED.equals (state); SDcard Mount performs the following storage operation
3. Specify directory: File File=new file (Environment.getexternalstoragedirectory (). GetAbsolutePath () + "/cat.mp3");
4. Write Data: New FileOutputStream (file). writer ();
The data can be removed with FileInputStream.
Overview of how Android data is persisted