In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is Android file storage and SharedPreferences storage mode". In daily operation, I believe many people have doubts about Android file storage and SharedPreferences storage mode. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "what is Android file storage and SharedPreferences storage mode?" Next, please follow the editor to study!
Catalogue
Brief introduction of persistence Technology
file store
1. Store data in a file
two。 Read data from a file
SharedPreferences storage
1. Store data in SharedPreferences
two。 Read data from SharedPreferences
Brief introduction of persistence Technology
Data persistence means that the instantaneous data in memory is saved to the storage device to ensure that the data will not be lost even when the mobile phone or computer is turned off. The data stored in memory is in an instantaneous state, while the data stored in the storage device is in a persistent state. Persistence technology provides a mechanism to switch data between instantaneous and persistent states.
File storage 1. Store data in a file
File storage is the most basic data storage method in Android. It does not format the stored content. All data is saved to the file intact, which is suitable for storing some simple text data or binary data.
The Context class provides an openFileOutput () method that stores 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 ()}}
The openFileOutput () method receives two parameters:
The first parameter is the file name, which is used when the file is created. The specified file name may not contain a path because all files are stored in the / data/data//files/ directory by default
The second parameter is the operation mode of the file, which is mainly optional in MODE_PRIVATE and MODE_APPEND modes. The default is MODE_PRIVATE, which means that when the same file name is specified, the content written will overwrite the contents of the original file. MODE_APPEND means to append content to the file if it exists.
two。 Read data from a file
Similar to storing data in a file, the Context class provides an openFileInput () method for reading 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 ()}
The openFileInput () method takes only one parameter, the file name to be read, and then the system automatically loads the file in the / data/data//files directory
SharedPreferences storage
Unlike file storage, SharedPreferences uses key-value pairs to store data. That is, when you save a piece of data, you need to provide a corresponding key for that data. SharedPreferences supports many different types of data storage.
1. Store data in SharedPreferences
To use SharedPreferences, you first need to get the SharedPreferences object. The following two main methods are provided in Android to get SharedPreferences objects:
The getSharedPreferences () method in the Context class, which takes two parameters: the first parameter specifies the name of the SharedPreferences file, which is stored in the / data/data//shared_prefs/ directory; the second parameter specifies the mode of operation, which is currently optional only for MODE_PRIVATE, indicating that only the current application can read and write to the SharedPreferences file
The getPreferences () method in the Activity class, 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 the SharedPreferences
Once you have the SharedPreferences object, you can start storing data in 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 the stored data of the corresponding type. These get methods all accept two parameters: the first parameter is the key, and the second parameter is the default value, indicating what 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)}} On the "Android file storage and SharedPreferences storage is what is the end of the study, I hope to be able to solve your doubts." The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.