android进行数据的存储

  • Post author:
  • Post category:其他




android将数据存放在文件与外部SD卡中

1.内部存储

package com.example.administrator.myapplication;

import android.support.v7.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.TextView;
import android.widget.Toast;

import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText et_account; //账号输入框
    private EditText et_password; //密码输入框
    private Button btn_login;  //登录按钮
    private Button btn_massage;//显示数据
    private TextView textView;
    private TextView textView2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //通过工具类FileSaveQQ中的getUserInfo()方法获取qq账号与密码信息
        Map<String, String> userInfo = FileSaveQQ.getUserInfo(this);

        if (userInfo != null) {
            et_account.setText(userInfo.get("account")); //将获取的账号显示到界面上
            et_password.setText(userInfo.get("password")); //将获取的密码显示到界面上
        }
    }
    private void initView() {
        et_account = (EditText) findViewById(R.id.et_account);
        et_password = (EditText) findViewById(R.id.et_password);
        btn_login =(Button) findViewById(R.id.btn_login);
     btn_massage = (Button) findViewById(R.id.btn_show_massage);
        textView = (TextView) findViewById(R.id.tv_massage);
        textView2 = (TextView) findViewById(R.id.tv_massage2);
        //设置按钮的带年纪监听事件
        btn_login.setOnClickListener(this);
        btn_massage.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login: //当点击登录按钮时,获取界面上的输入的qq账号和密码
                String account = et_account.getText().toString();
                String password = et_password.getText().toString();
                //校验输入的账号与密码是否为空
                if (TextUtils.isEmpty(account)) {
                    Toast.makeText(this,"请输入QQ账号",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(this,"请输入密码账号",Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
                //保持用户信息
                boolean isSaveSuccess = FileSaveQQ.saveUserInfo(this,account,password);
                if (isSaveSuccess) {
                    Toast.makeText(this,"保持成功",Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(this,"保持失败",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_show_massage:
               Map<String, String> userInfo = FileSaveQQ.getUserInfo(this);
                String useraccoun = userInfo.get("account");
                String passwor = userInfo.get("password");
                et_account.setText(userInfo.get("account")); //将获取的账号显示到界面上
                textView.setText(useraccoun);

                textView2.setText(passwor);
                et_password.setText(userInfo.get("password")); //将获取的密码显示到界面上
                break;
        }
    }
}



保持类

package com.example.administrator.myapplication;

import android.content.Context;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2022/3/22.
 */

public class FileSaveQQ {
    public static boolean saveUserInfo(Context context, String account, String password){
        FileOutputStream fos = null;
        try{
            //获取文件的输入流对象fos
            fos = context.openFileOutput("data.text",Context.MODE_PRIVATE);
            //将数据转换为字节码转存到text文件中
            fos.write((account + ":" +password).getBytes());
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        } finally {
            try{
               if(fos != null){
                   fos.close();
               }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    //从data文件获取存储的qq账号和密码
    public static Map<String ,String> getUserInfo(Context context){
        String content = "";
        FileInputStream fis = null;
        try{
            //获取文件的输入流对象fis
            fis = context.openFileInput("data.text");
            //将数据转换为字节码转存到text文件中
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer); //通过read()方法读取字节码的数据
            content = new String (buffer); //将字节码转为字符串
            Map<String ,String> userMap = new HashMap<>();
            String[] infos = content.split(":");//将字符串以“:”分割形式存放数组内
            userMap.put("account",infos[0]); //将数字存放到userMap集合中
            userMap.put("password",infos[1]);
            return userMap;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        } finally {
            try{
                if(fis != null){
                    fis.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}



布局类

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:padding="10dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:background="@android:color/white"
        android:orientation="horizontal">
        <TextView
            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_account"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:background="@null"
            android:padding="10dp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@android:color/white"
        android:orientation="horizontal">
        <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_marginTop="25dp"
        android:background="#3C8DC4"
        android:text="登录"
        android:textColor="@android:color/white"
        android:textSize="20sp" />
    <Button
        android:id="@+id/btn_show_massage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:background="#3C8DC4"
        android:text="点击显示账户密码"
        android:textColor="@android:color/white"
        android:textSize="20sp" />
    <TextView
        android:id="@+id/tv_massage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/tv_massage2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>



外部CD卡存储

需要先开启权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
package com.example.administrator.myapplication2;

import android.support.v4.app.ActivityCompat;
import android.support.v7.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.TextView;
import android.widget.Toast;

import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText et_account; //账号输入框
    private EditText et_password; //密码输入框
    private Button btn_login;  //登录按钮
    private Button btn_massage;//显示数据
    private TextView textView;
    private TextView textView2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActivityCompat.requestPermissions(MainActivity.this,new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"},1); //获取读取SD卡权限

        initView();
        //通过工具类FileSaveQQ中的getUserInfo()方法获取qq账号与密码信息
        Map<String, String> userInfo = FileSaveQQ.getUserInfo(this);

        if (userInfo != null) {
            et_account.setText(userInfo.get("account")); //将获取的账号显示到界面上
            et_password.setText(userInfo.get("password")); //将获取的密码显示到界面上
        }
    }
    private void initView() {
        et_account = (EditText) findViewById(R.id.et_account);
        et_password = (EditText) findViewById(R.id.et_password);
        btn_login =(Button) findViewById(R.id.btn_login);
        btn_massage = (Button) findViewById(R.id.btn_show_massage);
        textView = (TextView) findViewById(R.id.tv_massage);
        textView2 = (TextView) findViewById(R.id.tv_massage2);
        //设置按钮的带年纪监听事件
        btn_login.setOnClickListener(this);
        btn_massage.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login: //当点击登录按钮时,获取界面上的输入的qq账号和密码
                String account = et_account.getText().toString();
                String password = et_password.getText().toString();
                //校验输入的账号与密码是否为空
                if (TextUtils.isEmpty(account)) {
                    Toast.makeText(this,"请输入QQ账号",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(this,"请输入密码账号",Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
                //保持用户信息
                boolean isSaveSuccess = FileSaveQQ.saveUserInfo(account,password);
                if (isSaveSuccess) {
                    Toast.makeText(this,"保持成功",Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(this,"保持失败",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_show_massage:
                Map<String, String> userInfo = FileSaveQQ.getUserInfo(this);
                String useraccoun = userInfo.get("account");
                String passwor = userInfo.get("password");
                et_account.setText(userInfo.get("account")); //将获取的账号显示到界面上
                textView.setText(useraccoun);

                textView2.setText(passwor);
                et_password.setText(userInfo.get("password")); //将获取的密码显示到界面上
                break;
        }
    }
}



保持

package com.example.administrator.myapplication2;

import android.content.Context;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class FileSaveQQ {
    public static boolean saveUserInfo(String account, String password) {

        String state = Environment.getExternalStorageState();//读取SD卡状态
        if (state.equals(Environment.MEDIA_MOUNTED)) { //判断SD卡是否可用
            File SDPath = Environment.getExternalStorageDirectory();// 获取SD卡路径
            File file = new File(SDPath, "data.text");
            FileOutputStream fos = null;
            try {
                //获取文件的输入流对象fos
                fos = new FileOutputStream(file);
                //将数据转换为字节码转存到text文件中
                fos.write((account + ":" + password).getBytes());
                System.out.println(account+password);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            } finally {
                try {
                    if (fos != null) {
                        fos.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    //从data文件获取存储的qq账号和密码
    public static Map<String, String> getUserInfo(Context context) {
        String state = Environment.getExternalStorageState();//读取SD卡状态
        if (state.equals(Environment.MEDIA_MOUNTED)) { //判断SD卡是否可用
            File SDPath = Environment.getExternalStorageDirectory();// 获取SD卡路径
            File file = new File(SDPath, "data.text");
            BufferedReader br = null;
            String content = "";
            FileInputStream fis = null;
            try {
                //获取文件的输入流对象fis
                fis = new FileInputStream(file);
                br = new BufferedReader(new InputStreamReader(fis)); // 创建字符输入缓存流对象
                String accountdata = br.readLine(); //读取数据
                Map<String, String> userMap = new HashMap<>();
                String[] infos = accountdata.split(":");//将字符串以“:”分割形式存放数组内
                userMap.put("account", infos[0]); //将数字存放到userMap集合中
                userMap.put("password", infos[1]);
                return userMap;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            } finally {
                try {
                    if (br != null) {
                        br.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    if (fis != null) {
                        fis.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
        return null;
    }
}



布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:padding="10dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:background="@android:color/white"
        android:orientation="horizontal">
        <TextView
            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_account"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:background="@null"
            android:padding="10dp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@android:color/white"
        android:orientation="horizontal">
        <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_marginTop="25dp"
        android:background="#3C8DC4"
        android:text="登录"
        android:textColor="@android:color/white"
        android:textSize="20sp" />
    <Button
        android:id="@+id/btn_show_massage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:background="#3C8DC4"
        android:text="点击显示账户密码"
        android:textColor="@android:color/white"
        android:textSize="20sp" />
    <TextView
        android:id="@+id/tv_massage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/tv_massage2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

在这里插入图片描述



版权声明:本文为m0_46607044原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。