Overview of how Android data is persisted

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.