这个地方 包括我们保存以及我们使用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;
}
}
}