Introduction to various ways of data storage and access in Android development _android

Source: Internet
Author: User
Tags sqlite database
Data storage and access
Many times our software needs to store or revisit the processed data. Android provides a variety of ways for data storage, including the following:
File
Sharedpreferences
SQLite database
Contents provider (content Provider)
Internet

Using files for data storage
First of all to introduce how to use files to store data, activity provides a openfileoutput () method can be used to output data to a file, the specific implementation process and in the J2SE environment to save data to the file is the same.
Copy Code code as follows:

public class Fileactivity extends activity {
@Override public void OnCreate (Bundle savedinstancestate) {
...
FileOutputStream OutStream = this.openfileoutput ("Csdnt.txt", context.mode_private);
Outstream.write ("CSDN". GetBytes ());
Outstream.close ();
}
}

The first parameter of the Openfileoutput () method specifies the file name, cannot contain the path delimiter "/", and if the file does not exist, Android automatically creates it. The files created are saved in the/data/data/<package Name>/files directory, such as:/data/data/cn.csdn.action/files/csdn.txt, by clicking on the Eclipse menu window -' show View '-' other ', expand the Android folder in the Conversation window, select the File Explorer view below, and then expand/data/data/<package name> in the File Explorer view The file is visible to the/files directory.
The second parameter of the Openfileoutput () method is used to specify the mode of operation, with four modes, respectively: context.mode_private = 0
Context.mode_append = 32768
context.mode_world_readable = 1
Context.mode_world_writeable = 2

Context.mode_private: For the default mode of operation, the file is private data and can only be accessed by the application itself, in which the written content overwrites the contents of the original file, if you want to append the newly written content to the original file. You can use the Context.mode_append
Context.mode_append: Mode checks whether the file exists, appends to the file, or creates a new file.
Context.mode_world_readable and context.mode_world_writeable are used to control whether other applications 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.
If you want the file to be read and written by another application, you can pass in:
Openfileoutput ("Csdn.txt", context.mode_world_readable + context.mode_world_writeable);

Android has its own security model, and when the application (. apk) is installed, the system assigns him a userid, and when the application accesses other resources such as files, it needs userid matching. By default, any file created by the application, sharedpreferences, should be private (located in/data/data/<package name>/files) and not accessible by other programs. Unless context.mode_world_readable or context.mode_world_writeable are specified at the time of creation, only such other programs can access them correctly.

Read File contents
If you want to open a file that is private to the/data/data/<package name>/files directory, you can use the activity to provide the Openfileinput () method.
FileInputStream instream = This.getcontext (). Openfileinput ("Csdn.txt");
LOG.I ("Filetest", Readinstream (instream));
Readinstream () method please look at the comments below this page.

or use the absolute path of the file directly:
File File = new file ("/data/data/cn.csdn.action/files/csdn.txt");
FileInputStream instream = new FileInputStream (file);
LOG.I ("Filetest", Readinstream (instream));
Note: the "cn.csdn.action" in the file path above is the package where the application resides, and when you write the code you should replace the package you used for your own application.
For private files that can only be accessed by the application that created the file, if you want the file to be read and written by another application, you can specify context.mode_world_readable and Context.mode_world_writeable permissions when you create the file.

The activity also provides Getcachedir () and Getfilesdir () methods:
The Getcachedir () method is used to get the/data/data/<package Name>/cache directory
The Getfilesdir () method is used to get the/data/data/<package name>/files directory
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.