android--data Storage (1)

Source: Internet
Author: User

Looking at Android data storage today, there are three main ways to store Android data:

1, ordinary file storage.

With regard to normal file storage, the Android context class has a openfileoutput () method to return a FileOutputStream object that, through this object, can write data to a file using the Java stream. It mainly uses the Java flow idea, here I do not go into the depth of the explanation.

2.SharedPreferences Storage

Sharedpreference is the use of key-value pairs (the idea of the graph) to store data, which is present in XML format under the/data/data/<packagename>/shared_prefs folder. This allows for faster reading of data than normal file storage, as well as easier operation. It mainly supports the storage of the following six types of values--boolean, String, int, float, long, set<string>

Here's how to use the following sharedpreferences:

A.sharedpreferences Object Acquisition:

The Getsharedpreferences () method in the A.context class

The Getsharedpreferences () function accepts two parameters, the first one to specify the name of the file, if the file does not exist creates a new file, the second specifies the mode of operation: Mode_private (default, and 0 for the same effect) and mode_multi_ Ptocess.

The Getpreferences () method in B.activity

The Getpreferences () method has only one parameter (the mode of the specified operation), and the name of the file is replaced by the currently active class name.

The Getdefaultsharedpreferences () method in the C.preferencemanager class

The Getdecaultsharedpreferences () method is a static method that accepts a context parameter that uses the current app's package name as a prefix to name the sharedpreferences file. can be used across programs, to be verified

  B. Saving data to a sharedpreferences file

The first is sharedpreferences.editor Editor = getsharedpreferences ("data/* filename */", mode_private/* operation mode */). Edit (there should be three ways to get the object)

Inserting an object that requires a Sharedpreferences.editor class (As for why, as a beginner, requires a deeper understanding), here is the edit by getsharedpreferences method to obtain the object;

Then editor.putstring (key, value)/* Here key is a string type ... * * Save the value we want to write to the file into a key-value pair first in the editor object.

The last is Editor.commit (); The key-value pairs in our presence Editor object are submitted to the Sharedpreferences file.

C. reading data from a sharedpreferences file

Start is sharedpreferences pref = getsharedpreferences ("Data", mode_private) (there should also be three ways to do it)

Reading data from a file requires a Sharedpreferences object, which is obtained through getsharedpreferences.

Finally, string str = pref.getstring (key, DefaultValue), the value corresponding to the key key read out to the STR, when the key key corresponding to the value of NULL, will be assigned to the value of the key key DefaultValue.

Come here today, next time to carefully describe the use of the lightweight database SQLite ...

    

    

  

android--data Storage (1)

Related Article

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.