1. File storage
File storage is the most basic form of data storage in Android, it does not do any formatting of the stored content, all the data is stored intact in the file, so it is more suitable for storing some simple text data or binary data.
A Openfileoutput () method is provided in the Context class that can be used to store data in the specified file.
The Openfileoutput () method returns a FileOutputStream object that can be used to write data to a file using the Java stream.
The first parameter is the file name, the second parameter is the mode of operation of the files, there are two main modes to choose from, Mode_private (overwrite) and Mode_append (append).
Stores:
The Openfileoutput () method allows you to get a FileOutputStream object and then build a OutputStreamWriter object with it.
Then using OutputStreamWriter to build a BufferedWriter object, you can write the text content to the file by BufferedWriter.
Read:
A FileInputStream object was obtained through the Openfileinput () method, and then a InputStreamReader object was built with it.
Then we use InputStreamReader to build a BufferedReader object so that we can read through the BufferedReader line,
All text content in the file is read out and stored in a StringBuilder object, and finally the contents of the read are returned
2. Sharedpreference Storage
To use Sharedpreferences to store data, you first need to get to the Sharedpreferences object. There are three main methods available in Android to get Sharedpreferences objects.
1. The Getsharedpreferences () method in the Context class
This method receives two parameters, the first parameter specifies the name of the Sharedpreferences file, and if the specified file does not exist, it creates a
The second parameter specifies the mode of operation, and there are two main modes to choose from, Mode_private and mode_multi_process.
2. The Getpreferences () method in the Activity class
This method is similar to the Getsharedpreferences () method in the context, but it only receives an operation mode parameter, because using this method automatically takes the currently active class name as the sharedpreferences filename.
3. Getdefaultsharedpreferences () method in the 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. After getting the Sharedpreferences object,
You can start to store data in the Sharedpreferences file, which can be divided into three steps.
1. Call the edit () method of the Sharedpreferences object to get a Sharedpreferences.editor object.
2. Add data to the Sharedpreferences.editor object, such as adding a Boolean data using the Putboolean method, adding a string using the Putstring () method, and so on.
3. The commit () method is called to commit the data that was added, thus completing the data store operation.
Read:
Use the Getsharedpreferences () method to get the Sharedpreferences object, and then call its GetString (), GetInt (), and Getboolean () methods to get the contents that were stored earlier.
3. Database storage
Database Upgrade:
public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {
Db.execsql ("drop table if exists book");
Db.execsql ("drop table if exists Category");
OnCreate (DB);
}
4, stored in the SD card
Android Notes: Data storage