Data persistence means storing instantaneous data in memory on a storage device, ensuring that the data is not lost even when the phone or computer is turned off, that the data stored in memory is persistent, and that persistence provides a mechanism for transforming the data between the transient state and the persistent state. Android offers three ways to simply implement data persistence, namely file storage, Sharedpreference storage, and database storage. Of course, it can also be stored in an SD card, but it will be more secure than the above three methods.
One, the file storage
The Openfileoutput () method is provided in the context class, a total of two parameters, the first parameter is the file name (not including the path), all the files are stored by default to the/data/data/<packagename>/files/directory The second parameter is the operation mode of the file, with Mode_private (default), Mode_append Two, mode_private indicates that when the same file name is specified, the contents of the original file will be overwritten, Mode_ Append indicates that if the file already exists, a new content is added to the file and is created if it does not exist. This method returns the FileOutputStream object, which can then be written to the file using the Java stream.
Public voidSave () {String data= "Data to Save"; FileOutputStream out=NULL; BufferedWriter writer=NULL; Try{ out= Openfileoutput ("Data", context.mode_private); Writer=NewBufferedWriter (NewOutputStreamWriter (out)); Writer.write (data); }Catch(Exception e) {e.printstacktrace (); }finally { Try{ if(writer!=NULL) {writer.close (); } }Catch(Exception e) {e.printstacktrace ();} }}
The context also provides the Openfileinput () method for reading data from a file, accepting only one parameter, which is the name of the file to be read, and then the system will automatically go to/data/data/<package name>/files/ The directory goes down to load this file and returns the FileInputStream object.
PublicString Read () {FileInputStream fis=NULL; BufferedReader Reader=NULL; StringBuffer content=NewStringBuffer (); Try{FIS= This. Openfileinput ("Data"); Reader=NewBufferedReader (NewInputStreamReader (FIS)); String Line= ""; while((Line=reader.readline ())! =NULL) {content.append (line); } }Catch(IOException e) {e.printstacktrace (); }finally { Try { if(Reader! =NULL) {reader.close (); } }Catch(IOException e) {e.printstacktrace (); } } returncontent.tostring (); }
Second, sharedpreferences storage
Sharedpreferences is the way to store data using key-value pairs.
Android provides the following three ways to get Sharedpreferences objects:
The Getsharedpreferences () method in the 1.Context class receives two parameters, the first parameter specifies the name of the Sharedpreferences file, and the sharedpreferences file is stored in the/data/data/ <package name>/shared_prefs/directory; The second parameter specifies the mode of operation, and the default is mode_private means that only the current application can read and write to this sharedpreferences file. and the direct incoming 0 effect is the same, mode_multi_process is typically used for multiple processes to read and write to the same sharedpreferences file, similarly, mode_world_readable and Mode_world_ Writeable these two modes of operation have been deprecated in the Android 4.2 version.
Getpreferences () in the 2.Activity class
This method is similar to the getsharedpreferences () in the context, but it only receives an operation mode parameter, because using this method automatically takes the currently active class name to the sharedpreferences filename.
The Getdefaultsharedpreferences () method in the 3.PreferenceManager class
This is a static method that receives a context parameter and automatically uses the current application's package name as a prefix to name the sharedpreferences file.
Once the Sharedpreferences object has been obtained, the storage can be divided into three steps:
A. Call the edit () method of the Sharedpreferences object to get the Sharedpreferences.editor object.
B. Complete the addition of data by means of Sharedpreferences.editor objects such as putstring ().
C. Call the Commit () method of the Sharedpreferences.editor object to submit the data that was added.
READ: Gets the value of the corresponding key through the Sharedpreferences object, such as the GetString (String key,string defaultifnull) method.
Third, SQLite database storage
Android specifically provides a Sqliteopenhelper helper class that allows you to create and upgrade databases very easily. Sqliteopenhelper is an abstract class with two abstract methods OnCreate () and Onupgrade (), respectively, for creating and upgrading databases.
Both Getreadabledatabase () and getwritabledatabase () can create or open an existing database (open directly if the database already exists), or create a new database, and return an object that can read and write to the database. The difference is that when the database is not writable (such as disk space is full) the object returned by the Getreadabledatabase () method will open the database in a read-only manner, and the Getwritabledatabase () method will have an exception.
The Sqliteopenhelper construction method receives four parameters, the first parameter is the context, the second parameter is the database name, and the third parameter allows us to return a custom cursor when querying the data, which is usually passed to null. The fourth parameter represents the version number of the current database and can be used to perform an upgrade operation on the database. Once you have built an instance of Sqliteopenhelper, you can then call its getreadabledatabase () or Getwritabledatabase () method to create the database, and the database file will be stored in the/data/data/< In the package name>/databases/directory, the overridden OnCreate () method is also executed, so there is usually some logic to create the table here.
CREATE TABLE book (
ID Integer PRIMARY Key autoincrement,
Author text,
Price Real,
Pages Integer,
Name text
)
The integer in SQLite represents an integer, real represents a floating-point type, text represents a literal type, and a blob represents a binary type. Each sqlite will automatically generate a Android_metadata table.
Upgrade the database: in the Onupgrade () method to create a table or modify the table and other logic, when instantiating the implementation of the Sqliteopenhelper abstract class of the custom class, let its fourth parameter is the version number on the original base plus 1, you can let the Onupgrade () method to be executed.
Android Data storage