Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of android File Storage and SharedPreferences Storage

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "android file storage and SharedPreferences storage methods", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "android file storage and SharedPreferences storage methods" bar!

Demo

View the files saved by the device in [File Storage]

Catalogue

Activity_main

MainActivity

/ * File storage and SharedPreferences storage instance * / public class MainActivity extends AppCompatActivity {private EditText et_account, et_password; / / account input box, password input box private Button loginBtn; / / login button @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); initView () } private void initView () {et_account = findViewById (R.id.et_account); et_password = findViewById (R.id.et_password); loginBtn = findViewById (R.id.btn_login) / / Click the listening event loginBtn.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View view) {switch (view.getId ()) {case R.id.btn_login: / / when the login button is clicked Get the QQ account number and password entered on the interface String account = et_account.getText () .toString () .trim () String password = et_password.getText () .toString (); / / verify whether the account and password entered are empty if (TextUtils.isEmpty (account)) {Toast.makeText (getApplicationContext (), "Please enter QQ account", Toast.LENGTH_SHORT) .show () Return;} if (TextUtils.isEmpty (password)) {Toast.makeText (getApplicationContext (), "Please enter password", Toast.LENGTH_SHORT) .show (); return } / / File storage / / saveOfFile (account,password); / / SharedPreferences storage saveOfSharedPreferences (account,password); break;}) } / / SharedPreferences stores private void saveOfSharedPreferences (String account, String password) {/ / get QQ account and password information SharedPreferences userInfo=SPSaveQQ.getUserInfo (getApplicationContext ()); if (userInfo.getString ("username", ")! = null&&userInfo.getString (" pwd ",")! = null) {String username = userInfo.getString ("username", "") / / read account String pwd = userInfo.getString ("pwd", "); / / read password Log.i (" user "," read user information "); Log.i (" user "," username: "+ username +", pwd: "+ pwd) If (username.equals (account) & & pwd.equals (password)) {Toast.makeText (getApplicationContext (), username+ "login successful!", Toast.LENGTH_SHORT). Show ();} else {Log.i ("user", "user or password error!") / / Save user information boolean isSaveSuccess = SPSaveQQ.saveUserInfo (getApplicationContext (), account, password); if (isSaveSuccess) {Toast.makeText (getApplicationContext (), "saved successfully!", Toast.LENGTH_SHORT) .show () } else {Toast.makeText (getApplicationContext (), "Save failed!", Toast.LENGTH_SHORT). Show ();} else {/ / Save user information boolean isSaveSuccess = SPSaveQQ.saveUserInfo (getApplicationContext (), account, password) If (isSaveSuccess) {Toast.makeText (getApplicationContext (), "Save successful!", Toast.LENGTH_SHORT). Show ();} else {Toast.makeText (getApplicationContext (), "Save failed!", Toast.LENGTH_SHORT) .show () } / / File Storage private void saveOfFile (String account, String password) {/ / get QQ account and password information Map userInfo = FileSaveQQ.getUserInfo (getApplicationContext ()); if (userInfo! = null) {/ / display the acquired account to the interface et_account.setText (userInfo.get ("account")) / / display the obtained password to the interface et_password.setText (userInfo.get ("password")); Toast.makeText (getApplicationContext (), userInfo.get ("account") + "login successful!", Toast.LENGTH_SHORT). Show ();} else {/ / Save user information boolean isSaveSuccess = FileSaveQQ.saveUserInfo (getApplicationContext (), account, password) If (isSaveSuccess) {Toast.makeText (getApplicationContext (), "Save successful!", Toast.LENGTH_SHORT). Show ();} else {Toast.makeText (getApplicationContext (), "Save failed!", Toast.LENGTH_SHORT). Show ();}

FileSaveQQ

/ * realize the file access and reading function of QQ account and password * / public class FileSaveQQ {/ * save user information * @ param context * @ param account account * @ param password password * @ return * / public static boolean saveUserInfo (Context context, String account, String password) {/ / the output stream object of the file FileOutputStream fos = null Try {/ / gets the output stream object fos of the file, which can only be read and written by the current program fos = context.openFileOutput ("user.txt", Context.MODE_PRIVATE); / / convert the data to bytecode and write it to fos.write in the user.txt file ((account + ":" + password) .getBytes ()) Return true;} catch (Exception e) {e.printStackTrace (); return false;} finally {try {if (fos! = null) {fos.close ();}} catch (IOException e) {e.printStackTrace () } / * get the stored user information from the user.txt file * @ param context * @ return * / public static Map getUserInfo (Context context) {String content = ""; / / the input stream object FileInputStream fis = null of the file Try {/ / get the input stream object of the file fis fis = context.openFileInput ("user.txt"); / / convert the data in the input stream object into the bytecode form byte [] buffer = new byte [fis.available ()]; / / read the data fis.read (buffer) in the bytecode through the read () method / / convert the acquired bytecode to the string content = new String (buffer); Map userMap = new HashMap (); String [] infos = content.split (":"); / / put the account password userMap.put ("account", infos [0]); userMap.put ("password", infos [1]) Log.i ("user", "read user information"); Log.i ("user", "account:" + infos [0] + ", password:" + infos [1]); return userMap;} catch (Exception e) {e.printStackTrace (); return null } finally {try {if (fis! = null) {fis.close ();}} catch (IOException e) {e.printStackTrace ();}

SPSaveQQ

/ * access and read functions of QQ account and password SharedPreferences * / public class SPSaveQQ {/ * Save user information * / public static boolean saveUserInfo (Context context, String account, String password) {SharedPreferences sharedPreferences = null; try {sharedPreferences = context.getSharedPreferences ("user", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit () / / get editor editor.putString ("username", account); editor.putString ("pwd", password); editor.commit (); / / submit modification return true;} catch (Exception e) {e.printStackTrace (); return false;} finally {if (sharedPreferences! = null) {return true } return false;}} / * read user information * / public static SharedPreferences getUserInfo (Context context) {SharedPreferences userInfo = context.getSharedPreferences ("user", Context.MODE_PRIVATE); return userInfo }} Thank you for reading, the above is the content of "android file storage and SharedPreferences storage method". After the study of this article, I believe you have a deeper understanding of the method of android file storage and SharedPreferences storage, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report