android将数据存放在文件与外部SD卡中 【技术|android进行数据的存储】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> 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> 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> 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> 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();
}
}
}
}
布局类
外部CD卡存储
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> 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> 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> 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 = https://www.it610.com/article/br.readLine();
//读取数据
Map, 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;
}
}
布局
文章图片