Android的文件存储,有I/O流的方式存储,与java一样,还有一种Android自己的SharePreferences存储方法。
下面看一个例子:
用I/O流的方式存储方法和SharePreferences存储方法,存放QQ账号和密码,再次进入页面时,把存储在文件中的账号密码显示在上面。
activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#E6E6E6"
- android:orientation="vertical">
-
- <ImageView
- android:id="@+id/iv"
- android:layout_width="70dp"
- android:layout_height="70dp"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="40dp"
- android:src="@drawable/head"
- />
- <LinearLayout
- android:id="@+id/ll_number"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/iv"
- android:layout_centerVertical="true"
- android:layout_marginBottom="5dp"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:layout_marginTop="15dp"
- android:background="#ffffff">
-
- <TextView
- android:id="@+id/tv_number"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:padding="10dp"
- android:text="账号"
- android:textColor="#000"
- android:textSize="20sp"/>
- <EditText
- android:id="@+id/et_number"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="5dp"
- android:background="@null"
- android:padding="10dp"/>
- </LinearLayout>
-
- <LinearLayout
- android:id="@+id/ll_password"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/ll_number"
- android:layout_centerVertical="true"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:background="#ffffff">
-
- <TextView
- android:id="@+id/tv_password"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:padding="10dp"
- android:text="密码"
- android:textColor="#000"
- android:textSize="20sp"/>
- <EditText
- android:id="@+id/et_password"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="5dp"
- android:background="@null"
- android:inputType="textPassword"
- android:padding="10dp"/>
- </LinearLayout>
-
- <Button
- android:id="@+id/btn_login"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/ll_password"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:layout_marginTop="50dp"
- android:background="#3c8dc4"
- android:text="登录"
- android:textColor="#ffffff"
- android:textSize="20sp"
- />
- </RelativeLayout>
MainActivity.java
- package com.example.saveqq;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
-
- import java.util.Map;
-
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
-
- private EditText user;
- private EditText password;
- private Button button;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //1.初始化view
- initView();
-
- //2.若用户保存了信息,进行数据回写
- //I/O流方法
- Map<String,String> userInfo = FileSaveQQ.getUserInfo(this);
-
- //SharedPreferences的方法
- /* Map<String,String> userInfo = SpSaveQQ.getUserInfo(this);*/
- if ((userInfo!=null)){
- user.setText(userInfo.get("user"));
- password.setText(userInfo.get("password"));
- }
- }
-
- private void initView() {
- //控件的初始化
- user = (EditText)findViewById(R.id.et_number);
- password = (EditText)findViewById(R.id.et_password);
- button = (Button) findViewById(R.id.btn_login);
- //2.设置按钮点击事件
- button.setOnClickListener(this);
- }
-
- @Override
- public void onClick(View v) {
- //1.点击获取账号密码
- String s_user = user.getText().toString().trim();
- String s_password = password.getText().toString().trim();
- //2.检查用户名和密码是否为空
- if (TextUtils.isEmpty(s_user)){
- Toast.makeText(this,"请输入QQ账号",Toast.LENGTH_LONG).show();
- return;
- }
- if (TextUtils.isEmpty(s_password)){
- Toast.makeText(this,"请输入QQ密码",Toast.LENGTH_LONG).show();
- return;
- }
- Toast.makeText(this,"登陆成功",Toast.LENGTH_LONG).show();
-
- //3.保存用户信息
- //I/O流的方法
- boolean isSaveSuccess = FileSaveQQ.saveUserInfo(this,s_user,s_password);
- //用SharedPreferences的方法
- /* boolean isSaveSuccess = SpSaveQQ.saveUserInfo(this,s_user,s_password);*/
- if (isSaveSuccess){
- Toast.makeText(this,"保存成功",Toast.LENGTH_LONG).show();
- }else{
- Toast.makeText(this,"保存失败",Toast.LENGTH_LONG).show();
- }
- }
- }
用i/o流方法
FileSaveQQ.java
- package com.example.saveqq;
-
- import android.content.Context;
-
-
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Map;
-
- public class FileSaveQQ {
- //保存QQ账号和密码到data.txt
- public static boolean saveUserInfo(Context context,String user,String password){
- try {
- //1.通过上下文获取文件输出流
- FileOutputStream fos = context.openFileOutput("data.txt",context.MODE_APPEND);
- //2.把数据写到文件中
- fos.write((user+":"+password).getBytes());
- fos.close();
- return true;
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- }
-
- public static Map<String,String> getUserInfo(Context context){
- String content = "";
- try {
- FileInputStream fis = context.openFileInput("data,txt");
- byte[] buffer = new byte[fis.available()];
- fis.read(buffer);
- Map<String,String> userMap = new HashMap<String, String>();
- content = new String(buffer);
- String[] infos = content.split(":");
- userMap.put("user",infos[0]);
- userMap.put("password",infos[1]);
- fis.close();
- return userMap;
- } catch (IOException e ) {
- return null;
- }
-
-
- }
- }
用SharedPreferences的方法
SpSaveQQ.java
- package com.example.saveqq;
-
- import android.annotation.SuppressLint;
- import android.content.Context;
- import android.content.SharedPreferences;
-
- import java.util.HashMap;
- import java.util.Map;
-
- //保存QQ账号和密码到data.xml中
- public class SpSaveQQ {
- public static boolean saveUserInfo(Context context,String username,String password){
- SharedPreferences sp = context.getSharedPreferences("data",context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sp.edit();
- editor.putString("username",username);
- editor.putString("password",password);
- editor.commit();
- return true;
- }
-
- //从data.xml文件中获取存储的QQ账号和密码
- public static Map<String,String> getUserInfo(Context context){
- SharedPreferences sp = context.getSharedPreferences("data",context.MODE_PRIVATE);
- String username = sp.getString("username","");
- String password = sp.getString("password","");
- Map<String,String> userMap = new HashMap<>();
- userMap.put("username",username);
- userMap.put("password",password);
- return userMap;
- }
- }
运行截图:


重新进入页面:

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