保存到data里面
方法一:这种保存方法并不提倡使用,因为绝对路径换一个环境可能就不能使用了
//传过来的参数用##拼接成一个字符串
String info=name+"##"+password;
// 创建一个文件,用于存放密码账户
File file=new File("data/data/com.example.test11/info.txt");
try {
//文件输出流,用于往内存中写数据
FileOutputStream fos = new FileOutputStream(file);
fos.write(info.getBytes());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我理解的输入输出流的方法是:以自己为中心,如果你要向外界,向文档里面写东西,就是你向外边输出,这就是输出流,使用FileOutputStream
从内存里提取出来东西让我知道,是输入,输入流FileInputStream
方法二:使用上下文获取相对路径,安全
String temp=name+"##"+password;
try {
FileOutputStream fos = context.openFileOutput("info2.txt",Context.MODE_PRIVATE);
fos.write(temp.getBytes());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
保存到SD卡
String temp=name+"##"+password;
//获取sd卡的相对路径
File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/info.txt");
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(temp.getBytes());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
记住密码,再次登陆时账号和密码能够显示出来
//保存密码的路径
File file=new File("data/data/com.example.test11/info.txt");
try {
// 文件输入流,为文本内容从内存里读过来
FileInputStream fis=new FileInputStream(file);
// 开始从内存里读过来
BufferedReader reader=new BufferedReader(new InputStreamReader(fis));
String temp=reader.readLine();
String[] result = temp.split("##");
return result;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
给大家分享一个专注于分享编程知识和有趣案例的公众号:oldCode