首页 > 其他 > 详细

数据存储--file

时间:2017-02-25 12:18:33      阅读:104      评论:0      收藏:0      [点我收藏+]

//保存用户名密码
public static boolean saveUserInfo_android(Context context,String username, String password) {

try{
String userinfo = username + "##"+ password;//封装用户名密码
//得到私有目录下一个文件写入流; name : 私有目录文件的名称 mode: 文件的操作模式, 私有,追加,全局读,全局写
FileOutputStream fileOutputStream = context.openFileOutput("userinfo.txt", Context.MODE_PRIVATE);
fileOutputStream.write(userinfo.getBytes());//将用户名密码写入文件
fileOutputStream.close();
return true;
}catch (Exception e) {
e.printStackTrace();
}

return false;
}


//获取用户名密码
public static Map<String ,String> getUserInfo_android(Context context){

try{

//通过context对象获取一个私有目录的文件读取流
FileInputStream fileInputStream = context.openFileInput("userinfo.txt");
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;

}catch (Exception e) {
e.printStackTrace();
}
return null;

}

数据存储--file

原文:http://www.cnblogs.com/wwttsqt/p/6441362.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!