经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android开发文件存储实例
来源:jb51  时间:2021/11/9 15:50:44  对本文有异议

Android的文件存储,有I/O流的方式存储,与java一样,还有一种Android自己的SharePreferences存储方法。

下面看一个例子:

用I/O流的方式存储方法和SharePreferences存储方法,存放QQ账号和密码,再次进入页面时,把存储在文件中的账号密码显示在上面。

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#E6E6E6"
  6. android:orientation="vertical">
  7.  
  8. <ImageView
  9. android:id="@+id/iv"
  10. android:layout_width="70dp"
  11. android:layout_height="70dp"
  12. android:layout_centerHorizontal="true"
  13. android:layout_marginTop="40dp"
  14. android:src="@drawable/head"
  15. />
  16. <LinearLayout
  17. android:id="@+id/ll_number"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:layout_below="@id/iv"
  21. android:layout_centerVertical="true"
  22. android:layout_marginBottom="5dp"
  23. android:layout_marginLeft="10dp"
  24. android:layout_marginRight="10dp"
  25. android:layout_marginTop="15dp"
  26. android:background="#ffffff">
  27.  
  28. <TextView
  29. android:id="@+id/tv_number"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:padding="10dp"
  33. android:text="账号"
  34. android:textColor="#000"
  35. android:textSize="20sp"/>
  36. <EditText
  37. android:id="@+id/et_number"
  38. android:layout_width="match_parent"
  39. android:layout_height="wrap_content"
  40. android:layout_marginLeft="5dp"
  41. android:background="@null"
  42. android:padding="10dp"/>
  43. </LinearLayout>
  44.  
  45. <LinearLayout
  46. android:id="@+id/ll_password"
  47. android:layout_width="match_parent"
  48. android:layout_height="wrap_content"
  49. android:layout_below="@id/ll_number"
  50. android:layout_centerVertical="true"
  51. android:layout_marginLeft="10dp"
  52. android:layout_marginRight="10dp"
  53. android:background="#ffffff">
  54.  
  55. <TextView
  56. android:id="@+id/tv_password"
  57. android:layout_width="wrap_content"
  58. android:layout_height="wrap_content"
  59. android:padding="10dp"
  60. android:text="密码"
  61. android:textColor="#000"
  62. android:textSize="20sp"/>
  63. <EditText
  64. android:id="@+id/et_password"
  65. android:layout_width="match_parent"
  66. android:layout_height="wrap_content"
  67. android:layout_marginLeft="5dp"
  68. android:background="@null"
  69. android:inputType="textPassword"
  70. android:padding="10dp"/>
  71. </LinearLayout>
  72.  
  73. <Button
  74. android:id="@+id/btn_login"
  75. android:layout_width="match_parent"
  76. android:layout_height="wrap_content"
  77. android:layout_below="@id/ll_password"
  78. android:layout_marginLeft="10dp"
  79. android:layout_marginRight="10dp"
  80. android:layout_marginTop="50dp"
  81. android:background="#3c8dc4"
  82. android:text="登录"
  83. android:textColor="#ffffff"
  84. android:textSize="20sp"
  85. />
  86. </RelativeLayout>

MainActivity.java

  1. package com.example.saveqq;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.os.Bundle;
  6. import android.text.TextUtils;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.Toast;
  11.  
  12. import java.util.Map;
  13.  
  14. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  15.  
  16. private EditText user;
  17. private EditText password;
  18. private Button button;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. //1.初始化view
  24. initView();
  25.  
  26. //2.若用户保存了信息,进行数据回写
  27. //I/O流方法
  28. Map<String,String> userInfo = FileSaveQQ.getUserInfo(this);
  29. //SharedPreferences的方法
  30. /* Map<String,String> userInfo = SpSaveQQ.getUserInfo(this);*/
  31. if ((userInfo!=null)){
  32. user.setText(userInfo.get("user"));
  33. password.setText(userInfo.get("password"));
  34. }
  35. }
  36.  
  37. private void initView() {
  38. //控件的初始化
  39. user = (EditText)findViewById(R.id.et_number);
  40. password = (EditText)findViewById(R.id.et_password);
  41. button = (Button) findViewById(R.id.btn_login);
  42. //2.设置按钮点击事件
  43. button.setOnClickListener(this);
  44. }
  45.  
  46. @Override
  47. public void onClick(View v) {
  48. //1.点击获取账号密码
  49. String s_user = user.getText().toString().trim();
  50. String s_password = password.getText().toString().trim();
  51. //2.检查用户名和密码是否为空
  52. if (TextUtils.isEmpty(s_user)){
  53. Toast.makeText(this,"请输入QQ账号",Toast.LENGTH_LONG).show();
  54. return;
  55. }
  56. if (TextUtils.isEmpty(s_password)){
  57. Toast.makeText(this,"请输入QQ密码",Toast.LENGTH_LONG).show();
  58. return;
  59. }
  60. Toast.makeText(this,"登陆成功",Toast.LENGTH_LONG).show();
  61.  
  62. //3.保存用户信息
  63. //I/O流的方法
  64. boolean isSaveSuccess = FileSaveQQ.saveUserInfo(this,s_user,s_password);
  65. //用SharedPreferences的方法
  66. /* boolean isSaveSuccess = SpSaveQQ.saveUserInfo(this,s_user,s_password);*/
  67. if (isSaveSuccess){
  68. Toast.makeText(this,"保存成功",Toast.LENGTH_LONG).show();
  69. }else{
  70. Toast.makeText(this,"保存失败",Toast.LENGTH_LONG).show();
  71. }
  72. }
  73. }

用i/o流方法

FileSaveQQ.java

  1. package com.example.saveqq;
  2.  
  3. import android.content.Context;
  4.  
  5.  
  6. import java.io.FileInputStream;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileOutputStream;
  9. import java.io.IOException;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12.  
  13. public class FileSaveQQ {
  14. //保存QQ账号和密码到data.txt
  15. public static boolean saveUserInfo(Context context,String user,String password){
  16. try {
  17. //1.通过上下文获取文件输出流
  18. FileOutputStream fos = context.openFileOutput("data.txt",context.MODE_APPEND);
  19. //2.把数据写到文件中
  20. fos.write((user+":"+password).getBytes());
  21. fos.close();
  22. return true;
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. return false;
  26. }
  27. }
  28.  
  29. public static Map<String,String> getUserInfo(Context context){
  30. String content = "";
  31. try {
  32. FileInputStream fis = context.openFileInput("data,txt");
  33. byte[] buffer = new byte[fis.available()];
  34. fis.read(buffer);
  35. Map<String,String> userMap = new HashMap<String, String>();
  36. content = new String(buffer);
  37. String[] infos = content.split(":");
  38. userMap.put("user",infos[0]);
  39. userMap.put("password",infos[1]);
  40. fis.close();
  41. return userMap;
  42. } catch (IOException e ) {
  43. return null;
  44. }
  45.  
  46.  
  47. }
  48. }

用SharedPreferences的方法

SpSaveQQ.java

  1. package com.example.saveqq;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.content.Context;
  5. import android.content.SharedPreferences;
  6.  
  7. import java.util.HashMap;
  8. import java.util.Map;
  9.  
  10. //保存QQ账号和密码到data.xml中
  11. public class SpSaveQQ {
  12. public static boolean saveUserInfo(Context context,String username,String password){
  13. SharedPreferences sp = context.getSharedPreferences("data",context.MODE_PRIVATE);
  14. SharedPreferences.Editor editor = sp.edit();
  15. editor.putString("username",username);
  16. editor.putString("password",password);
  17. editor.commit();
  18. return true;
  19. }
  20.  
  21. //从data.xml文件中获取存储的QQ账号和密码
  22. public static Map<String,String> getUserInfo(Context context){
  23. SharedPreferences sp = context.getSharedPreferences("data",context.MODE_PRIVATE);
  24. String username = sp.getString("username","");
  25. String password = sp.getString("password","");
  26. Map<String,String> userMap = new HashMap<>();
  27. userMap.put("username",username);
  28. userMap.put("password",password);
  29. return userMap;
  30. }
  31. }

运行截图:

重新进入页面:

完成。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号