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    <strong>writes bytes to a file </strong> 所以
<b>fileOutputStream.write(userinfo.getBytes());</b>

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<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("username", split[0]);
            hashMap.put("password", split[1]);
            bufferedReader.close();
            fileInputStream.close();
            return hashMap;

参看Java内容

Mainactivity部分


         Map<String ,String>map=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);
        }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,860评论 0 23
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,976评论 0 0
  • 关键词:银河、天空、月球、好似还有风。 站在老家平房房顶,好似是有风,超级大的风,对风的记忆仅此感觉而已。 抬...
    水影haha阅读 295评论 0 0
  • 想我吗你
    李忠旋风阅读 130评论 0 0
  • 7月中旬的时候,单位安排了一项很丑的青蛙任务给我,为院史宣传片写脚本。我从来没有从事过这方面工作任务经验,根本不知...
    跟娟姐学蜕变阅读 1,758评论 2 4

友情链接更多精彩内容