Detailed explanation of I / O operation of Android data persistence

This paper describes the I / O operation of Android data persistence. Share with you for your reference, as follows:

In the previous article, we briefly introduced the operation of file. In this section, we will talk about the basic operation of using the built-in object of Android platform to realize the file

There are two main classes: openfileoutput (write) and openfileinput (read)

Write data codes into the file as follows:

Read the code in the text as follows:

From the above code, we can see that the usage of openfileoutput / openfileinput is very similar to the previous usage of SharedPreferences. Here are the similarities and differences between the two:

Creation of SharedPreferences object:

The first parameter of getsharedpreferences method is to specify the file name to be saved in the phone memory (excluding the extension, the extension is XML), and the second parameter is to represent the file attribute set by SharedPreferences object when creating XML file;

Context.MODE_ Private = 0 (default), which means that the file is private data and can only be accessed by the application itself. In this mode, the written content will overwrite the content of the original file

Context.MODE_ APPEND = 32768 Context.MODE_ WORLD_ READABLE = 1 Context.MODE_ WORLD_ WRITEABLE = 2

How the openfileoutput method returns an OutputStream object:

The file name specified in the first parameter of openfileoutput has an extension, and the second parameter is the same as that of getsharedpreferences; From these two methods, the first parameter only specifies the file name and does not contain the path of the file. Therefore, @ h_ 419_ 49 @ these two methods can only save files in a fixed path in the memory of the phone. For large files, there may not be enough memory:

SharedPreferences saves the XML file in: / data / data / < package name > / shared_ prefs

Openfileoutput saves the file in: / data / data / < package name > / files

Activity also provides getcachedir() and getfilesdir() methods:

The getcachedir () method is used to obtain the: / data / data / < package name > / cache directory

The getfilesdir () method is used to obtain the: / data / data / < package name > / files directory.

For more Android related content, readers who are interested can view the special topics of this site: summary of Android file operation skills, summary of Android database operation skills, summary of activity operation skills of Android programming, summary of SD card operation methods of Android programming and development, introduction and advanced tutorial of Android development, summary of Android resource operation skills Android view skills summary and Android control usage summary

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>