Android-存储用户登录信息

用户登录

目的:
完成EditText中输入用户名和密码,并存储在私人目录中以备调用

1、 存储输入的内容:

利用Android Context类openFileOutput方法
    FileOutputStream fileOutputStream = context.openFileOutput("userinfo", context.MODE_PRIVATE); 

关于openFileOutput文档注释:

Open a private file associated with this Context's application package for writing. Creates the file if it doesn't already exist.

No permissions are required to invoke this method, since it uses internal storage.

Return:

The resulting FileOutputStream.

关于FileOutputStream    writes bytes to a file 所以
fileOutputStream.write(userinfo.getBytes());

An output stream that writes bytes to a file. If the output file exists, it can be replaced or appended to. If it does not exist, a new file will be created.


2、 回显存储的信息

              FileInputStream fileInputStream = context.openFileInput("userinfo");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
            String readLine = bufferedReader.readLine();
            String[] split = readLine.split("##");
            HashMap hashMap = new HashMap();
            hashMap.put("username", split[0]);
            hashMap.put("password", split[1]);
            bufferedReader.close();
            fileInputStream.close();
            return hashMap;

参看Java内容

Mainactivity部分


         Mapmap=UserInfoUtil.getUserInfo(mContext);
        if(map !=null){
            String username = map.get("username");
            String password = map.get("password");
            et_username.setText(username);
            et_password.setText(password);
            
            cb_remPass.setChecked(true);
        }

你可能感兴趣的:(Android-存储用户登录信息)