cache 数据保存到cache

这个地方  包括我们保存以及我们使用cache里面的数据。

public class SaveFileService {



// 存储


/**
* 存储操作

* @param username
*            帐号名
* @param password
*            密码
* @return
*/


public static boolean saveFile(Context context, String username,
String password)


{
// we do chicken right
// context : 上下文,就是为我们提供了一些简便的API
// 返回的一个文件的绝对路径


// 创建文件对象
// File file = new File("/data/data/com.itheima.login", "info.txt");


// 创建文件对象,通过file目录
// File file = new File(context.getFilesDir(), "info.txt");


// 创建文件对象,通过cache目录
File file = new File(context.getCacheDir(), "info.txt");


try {
// 文件输出流
FileOutputStream fos = new FileOutputStream(file);


// 写数据,zhangsan##123
fos.write((username + "##" + password).getBytes());


// 关闭输出流


fos.close();


return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}


}


// 回显数据


public static Map<String, String> getUserInfo(Context context) {


// 文件对象


File file = new File(context.getCacheDir(), "info.txt");
try {
// 输入流
FileInputStream fis = new FileInputStream(file);
// br对象
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
// 读里面的内容,zhangsan##123
String result = br.readLine();
// 拆分result,得到的是数组
String[] results = result.split("##");
// 得到map集合
Map<String, String> map = new HashMap<String, String>();
// 存储数据
map.put("username", results[0]);
map.put("password", results[1]);


return map;


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}


}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值