Android File Storage and SharedPreferences Storage
- 2021-12-21 05:05:28
- OfStack
Brief introduction of persistence technology
Data persistence refers to saving the instantaneous data in memory to the storage device, so as to ensure that the data will not be lost even if the mobile phone or computer is turned off. Data stored in memory is in an instantaneous state, while data stored in a storage device is in a persistent state. Persistence technology provides a mechanism to switch data between instantaneous state and persistent state
File storage
1. Store data in a file
File storage is the most basic data storage mode in Android, it does not format the stored content, all the data are intact saved to the file, suitable for storing some simple text data or binary data
Context
Class provides a
openFileOutput()
Method, you can store data in a specified file
fun save(inputText: String) {
try {
val output = openFileOutput("data", Context.MODE_PRIVATE)
val writer = BufferedWriter(OutputStreamWriter(output))
writer.use {
it.write(inputText)
}
} catch (e: IOException) {
e.printStackTrace()
}
}
openFileOutput()
Method accepts two parameters:
MODE_PRIVATE
And
MODE_APPEND
Two modes are optional, and the default is
MODE_PRIVATE
That means that when the same file name is specified, the contents written will overwrite the contents of the original file. And
MODE_APPEND
If the file exists, append the contents to the file
2. Read data from a file
Similar to storing data in a file,
Context
Class provides a
openFileInput()
Method to read data from a file
fun load(): String {
val content = StringBuilder()
try {
val input = openFileInput("data")
val reader = BufferedReader(InputStreamReader(input))
reader.use {
reader.forEachLine {
content.append(it)
}
}
} catch (e: IOException) {
e.printStackTrace()
}
return content.toString()
}
openFileInput()
Method takes only one parameter, the name of the file to read, and then the system automatically goes to/data/data/
<
package name
>
/files directory
SharedPreferences storage
Unlike file storage, SharedPreferences uses key-value pairs to store data. That is to say, when saving a piece of data, it is necessary to provide a corresponding key for this piece of data. SharedPreferences supports many different types of data storage.
1. Store data in SharedPreferences
To use SharedPreferences, you first get an SharedPreferences object. Android mainly provides the following two methods for obtaining SharedPreferences objects:
Context
Class in the
getSharedPreferences()
Method, which takes two parameters: The first parameter specifies the name of the SharedPreferences file, stored in/data/data/
<
package name
>
/shared_prefs/ directory; The second parameter specifies the operation mode, and currently only
MODE_PRIVATE
Optional, indicating that only the current application can read and write this SharedPreferences file
Activity
Class in the
getPreferences()
Method, which accepts only one operation mode parameter, because using this method automatically takes the class name of the current Activity as the file name of SharedPreferences
Once you get the SharedPreferences object, you can start storing data to the SharedPreferences
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
val editor = getSharedPreferences("data", Context.MODE_PRIVATE).edit()
editor.putString("name", "Tom")
editor.putInt("age", 28)
editor.putBoolean("married", false)
editor.apply()
}
}
2. Read data from SharedPreferences
The SharedPreferences object provides a series of get methods for reading stored data of the corresponding type. These get methods all take two parameters: the first parameter is the key, and the second parameter is the default value, which indicates what the default value will be returned if the passed key cannot find the corresponding value
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
val prefs = getSharedPreferences("data", Context.MODE_PRIVATE)
val name = prefs.getString("name", "")
val age = prefs.getInt("age", 0)
val married = prefs.getBoolean("married", false)
}
}