文件的保存
public static boolean saveUserInfo(Context context, String username, String password)
{
try
{
// 定义一个文件路径对象
File file = new File(context.getFilesDir(), "info.txt");
// 定义一个文件的写入流对象
FileOutputStream fos = new FileOutputStream(file);
// 用文件的写入流对象写数据到文件里面
fos.write((username + "##" + password).getBytes());
// 关闭文件的写入流
fos.close();
return true;
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
}
public static Map<String, String> getSavedUserInfo(Context context)
{
try
{
// 定义一个文件路径对象
File file = new File(context.getFilesDir(), "info.txt");
// 定义一个文件的读取流对象fis
FileInputStream fis = new FileInputStream(file);
// 定义一字符的读取流对象br
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
// 读取文本文件中的一行数据
String string = br.readLine();
// 使用split方法风格字符串,将分割之后的字符串数据保存到字符串数组里面
String[] infos = string.split("##");
// 定义一个Map集合,用来保存分割的字符串数组信息
Map<String, String> map = new HashMap<String, String>();
map.put("username", infos[0]);
map.put("password", infos[1]);
return map;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}原文地址:http://blog.csdn.net/feecooling/article/details/37872521