Android provides a variety of ways (XML file mode, file mode, database mode, etc.) for data storage. Developers can choose the appropriate storage method for data storage based on data type and design needs.
1.XML File Management
The XML file is more of a configuration file that is used to maintain the application or system. In Android, Goole provides sharedpreferences, the lightweight storage class as an upper-level interface for storing XML files, essentially <ket, value> value pairs.
Depending on whether the configuration information is open to other applications, Sharedpreferences provides mode_private., mode_world_readable two permissions. The operation of Sharedpreferences is divided into two ways: obtaining configuration information and storing configuration information. Here's how to get configuration information:
Sharedpreferences settings=getsharedpreferences (prefs_name,mode_private);
Boolean Silent=settings.getboolean ("Silentmode", false);
Storing configuration information and getting configuration information are slightly different, here's how to store configuration information:
Sharedpreference settings=getshatedpreferences (Prefs_name, mode_private);
Sharedpreference.editor Editor=settings.edit (); Get editor
Editor.putboolean ("Silentmode", Msilentmode);
Editor.commit (); Submit
If you want configuration information to be developed for other apps, you can use Mode_world_readable when you set permissions. When other applications want to obtain the appropriate configuration information, they must first obtain the appropriate context, by doing the following:
Context=createpackagecontext ("Com.miaozl.text", context.context_ignore_security);
if (context! = null) {
Sharedpreference settings=context.getsharedpreference (prefs_name,context.mode_world_readable);
Mtest=settings.getstring ("text", null);
}
It is necessary to note that support for Sharedpreferences is built into the preferenceactivity.
If you want listpreference to save or view the current selection, you can call the Listpreferences method as follows:
public void SetValue (String value)//corresponds to the value of the "Android:entries" property
public void Setvalueindex (int index)//The value of the "Android:entryvalues" property
Public String GetValue ()
Other preference operations are similar to listpreference.
2. Internal document management
For binary data, Android provides the means for internal storage, where developers can store data in the application's private space, avoiding access by other programs, and the data stored internally is deleted when the app is uninstalled.
The implementation of internal storage is very simple, and its permissions include Mode_private, Mode_append, Mode_world_readable, mode_world_writeable, and so on.
The internal storage is in the same directory as \data\data\com.company.packagename\files. If the file name is wors, the directory that is stored internally is \data\data\com.android.mms\files\words.
(1) Write data
Methods for writing String data:
FileOutputStream out=context.openfileoutput (file,context.mode_world_writeable);
Out.write (Capturearray.tostring (). GetBytes ());
Out.close ();
Methods for writing structure data:
private static void Writeconfiguration (context context, localeconfiguration configuration) {
DataOutputStream out = null;
Out=new DataOutputStream (Context.openfileoutput (PREFERENCES, mode_private));
Out.writeutf (Configuration.locale);
Out.writeint (CONFIGURATION.MCC);
Out.writeint (CONFIGURATION.MNC);
Out.flush ();
}
(2) reading data
Methods for reading string data:
StringBuilder sb=new StringBuilder ();
FileInputStream words=context.openfileinput ("word");
int C;
while ((C=words.read ())! =-1) {
if (c== ' \ r ' | | c== ' \ n ') {
String word=sb.tostring (). Trim ();
if (word.length () > 0) {
Mword.add (word);
}
Sb.setlength (0);
}else{
Sb.append ((char) c);
}
}
Words.close ();
Mwordcount=mwords.size ();
Ways to read data from a structure:
private static void Readconfiguration (context context, localeconfiguration configuration) {
DataInputStream In=null;
In=new DataInputStream (Context.openfileinput (PREFERENCES));
Configuration.locale=in,readutf ();
Configuration.mcc=in.readint ();
Configuration.mnc=in.readint ();
In.close ();
For static data that the app carries, it can be placed in the app's assets directory or res, raw directory.
For static data under the assets directory, there is a maximum limit of 1MB for a single file, read by: InputStream is=getassets (). Open ("Read_asset.txt");
For static data in res, raw directory, read by: Inputstream.inputstream=resources.openrawresource (r.raw.definitions);
Android also provides support for creating caches, which can get the app's cache path through Getcachedir (). Android empties the cache when the system is running low on free space, but it should not maintain too many caches for developers. As with internal storage, the cache directory is emptied and deleted when the app unloads.
3. external file Management
Android Data article