内部存储应用------保存QQ密码和账户

package xdglyt.software.jhwz.myapplication11;

import android.content.Context;

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

public class FileSaveClass {

    //保存密码和用户到文件中
    public static boolean saveUserInfo(Context context,String username,String password){

        try {
            FileOutputStream fos = context.openFileOutput("data.txt",Context.MODE_PRIVATE);
            //getBytes()得到一个操作系统默认的编码格式的字节数组
            //则采用的是Sytem.getProperty("file.encoding"),即当前文件的编码方式
            fos.write((username+":"+password).getBytes());
            fos.close();
            return true;
        }catch (Exception 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);
            content = new String(buffer);
            Map<String,String> userInfo = new HashMap<String,String>();
            String[] infos = content.split(":");
            userInfo.put("username",infos[0]);
            userInfo.put("password",infos[1]);
            fis.close();
            return userInfo;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
}

package xdglyt.software.jhwz.myapplication11;

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 org.w3c.dom.Text;

import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private EditText et1;
    private EditText et2;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et1 = (EditText)findViewById(R.id.et_username);
        et2 = (EditText)findViewById(R.id.et_password);
        btn = (Button)findViewById(R.id.button);

        Map<String,String>userInfo =FileSaveClass.getUserInfo(this);
        if(userInfo!=null){
            et1.setText(userInfo.get("username"));
            et2 .setText(userInfo.get("password"));
        }
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = et1.getText().toString();
                String password = et2.getText().toString();
                if(TextUtils.isEmpty(name)){
                    Toast.makeText(MainActivity.this,"请输入用户名!",Toast.LENGTH_LONG).show();
                    return;
                }
                if(TextUtils.isEmpty(password)){
                    Toast.makeText(MainActivity.this,"请输入密码!",Toast.LENGTH_LONG).show();
                    return;
                }
                Toast.makeText(MainActivity.this,"登陆成功!",Toast.LENGTH_LONG).show();
                boolean isSaveSuccess = FileSaveClass.saveUserInfo(MainActivity.this,name,password);
                if (isSaveSuccess){
                    Toast.makeText(MainActivity.this,"保存成功!",Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(MainActivity.this,"保存失败!",Toast.LENGTH_LONG).show();
                }
            }
        });

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

365JHWZGo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值