读写的文件以及拆分字符串的小问题

今天写了个Android登录界面的小程序,要求将用户名和密码在点击保存密码的CheckBox的情况下被保存在文件中。再次打开这个app的时候如果存信息的文件里面有东西就回显在对应的userName和passwd的EditText中。

但是在重新开启app的时候没有显示之前保存的信息。

分析:

之前代码:

在SaveUserInfo类里面:

<span style="font-size:18px;">public boolean saveInfo(Context contex, String name, String passwd){
		File file = new File(contex.getFilesDir(), fname);
		try {
			FileOutputStream fos = new FileOutputStream(file);
			fos.write((name+"$#"+passwd).getBytes());
			fos.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}</span>
在GetUserInfo类里面:

<span style="font-size:18px;">public Map<String, String> getInfo(Context context) {
		Map<String, String> map = new HashMap<String, String>();
		File file = new File(context.getFilesDir(), fname);
		try {
			FileInputStream fis = new FileInputStream(file);
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
			String[] infos = br.readLine().split("$#");
			for(String str : infos){
				Log.e(tag, str);
			}
			map.put("name", infos[0]);
			map.put("passwd", infos[1]);
			Log.e(tag, "Map got 'put'");
			br.close();
			fis.close();
			return map;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}</span>
改进:

<span style="font-size:18px;">public Map<String, String> getInfo(Context context) {
		Map<String, String> map = new HashMap<String, String>();
		File file = new File(context.getFilesDir(), fname);
		try {
			FileInputStream fis = new FileInputStream(file);
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
			String[] infos = br.readLine().split("\\$\\#");
			for(String str : infos){
				Log.e(tag, str);
			}
			map.put("name", infos[0]);
			map.put("passwd", infos[1]);
			Log.e(tag, "Map got 'put'");
			br.close();
			fis.close();
			return map;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}</span>
加上转义字符就行了。
可能原因,写的时候用的字节流,读的时候用的字符流,拆分字符串的时候也是用的字符。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值