android数据存放map_Android存储数据到本地文件

这篇博客介绍了如何在Android应用中将用户输入的QQ号码和密码存储到本地文件,并在启动时从文件中读取数据以实现登录状态的保存。通过使用HashMap将数据保存为键值对,然后写入到文本文件,以及读取文件内容,实现数据的持久化存储。
摘要由CSDN通过智能技术生成

xml文件

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:padding="5dp"

>

android:id="@+id/qqnum"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:hint="请输入QQ"

android:inputType="number"

android:textSize="20dp"

/>

android:id="@+id/pass"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:hint="请输入密码"

android:inputType="textPassword"

android:textSize="20dp"

/>

android:id="@+id/rem"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="记住密码"

/>

android:id="@+id/Login"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="登录"/>

Utilspackage com.example.android22filelogin;

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.InputStreamReader;

import java.util.HashMap;

import java.util.Map;

import android.text.TextUtils;

public class Utils {

public static boolean saveUserInfo(String username,String pwd){

String data=username+"##"+pwd;

String path="/data/data/com.example.android22filelogin/data.txt";

try {

FileOutputStream out=new FileOutputStream(path);

out.write(data.getBytes());

out.flush();

out.close();

return true;

} catch (Exception e) {

e.printStackTrace();

}

return false;

}

public static Map getUserInfo(){

String path="/data/data/com.example.android22filelogin/data.txt";

try {

BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(path)));

String data=reader.readLine();

if(!TextUtils.isEmpty(data))

{

String [] datas=data.split("##");

Map userinfo=new HashMap();

userinfo.put("number", datas[0]);

userinfo.put("pwd", datas[1]);

return userinfo;

}

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

}

activitypublic class MainActivity extends Activity implements OnClickListener {

private EditText qqnum,pwd;

private CheckBox rem;

private Button but;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

qqnum=(EditText)findViewById(R.id.qqnum);

pwd=(EditText)findViewById(R.id.pass);

rem=(CheckBox)findViewById(R.id.rem);

but=(Button)findViewById(R.id.Login);

but.setOnClickListener(this);

//回显数据

Map userinfo=Utils.getUserInfo();

if(userinfo!=null)

{

qqnum.setText(userinfo.get("number"));

pwd.setText(userinfo.get("pwd"));

}

}

@Override

public void onClick(View v) {

//记住号码和密码

String num=qqnum.getText().toString();

String password=pwd.getText().toString();

if(TextUtils.isEmpty(num)||TextUtils.isEmpty(password))

{

Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_LONG).show();

return;

}

//判断是否记住密码

if(rem.isChecked())

{

boolean isSuccess=Utils.saveUserInfo(num, password);

Toast.makeText(this, isSuccess+"", Toast.LENGTH_LONG).show();

}

//登录成功

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值