Android Access Data mode: file, sharedpreferences

Source: Internet
Author: User
Tags sqlite database

Android Access Data mode: File, sharedpreferences, SQLite database, Content provider

File stream:

Use Java IO Stream to read and write files, file permissions default.

specify file permissions to write: Mode: File permissions mode_private files are private to the application, mode_world_readable are accessible to all applications, mode_world_writeable All applications can be written, mode_append if the file to be created is present, the newly written data does not overwrite the previous data.

Openfileoutput (name, mode);
Openfileinput (name);

Path related please view: http://blog.csdn.net/liang5630/article/details/41150677

Sharedpreferences:

It is a lightweight storage solution within a lightweight application and is especially suitable for storing software configuration parameters such as boolean,int,float,long,string data. Use Sharedpreferences to save data, Its essence is to use the XML file storage data, the path is:/data/data/<package name>/shared_prefs.

There are two ways to get sharedpreferences:

1 invoke the Getsharedpreferences () method of the context object

Blockquote style= "margin:0 0 0 40px; Border:none; padding:0px ">

2 call the Activity object's Getpreferences () method, file name: Current activity name. XML

/blockquote>

Differences between the two ways:

The Sharedpreferences object obtained by invoking the Getsharedpreferences () method of the context object can be shared by other components under the same application.

The Sharedpreferences object obtained by invoking the Getpreferences () method of the activity object can only be used in that activity.

Sharedpreferences Four modes of operation:

Context.mode_private

Context.mode_append

Context.mode_world_readable

Context.mode_world_writeable

Context.mode_private: Is the default mode of operation, which means that the file is private and can only be accessed by the app itself, where the contents of the original file will be overwritten by the written content

Context.mode_append: The mode checks whether the file exists, appends content to the file, or creates a new file.

Context.mode_world_readable and context.mode_world_writeable are used to control whether other apps have permission to read and write to the file.

Mode_world_readable: Indicates that the current file can be read by another application.

Mode_world_writeable: Indicates that the current file can be written by another application.

Mode_world_readable+mode_world_writeable readable and writable

Write Method:

Sharedpreferences sp=getsharedpreferences ("Aaa.txt", context.mode_private); Editor Editor=sp.edit (); editor.putstring ("Name", "Liang"); Editor.commit ();

Read method:

Sharedpreferences sp=getsharedpreferences ("Aaa.txt", context.mode_private); sp.getstring ("name", "read default value returned");



Android Access Data mode: file, sharedpreferences

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.