内部存储
步骤:1.
File file = new File(getFilesDir(),"文件名");
file.createNewFile();
FileWriter fw = new FileWriter(file);
fw.write(name);
fw.write(pwd);
fw.close();
步骤:2.
File file = new File(getFilesDir(),"itcast.txt"); FileWriter fw = null; try { file.createNewFile(); fw = new FileWriter(file); BufferedWriter writer = new BufferedWriter(fw); writer.write(name+"\n"); writer.write(pwd); writer. Close();
//登录
public void login(View v) {
//获得输入内容
String name = et_name.getText().toString().trim();
String pwd = et_pwd.getText().toString().trim();
//登录(登录前先判断输入是否为空,不是为空,就登录)
if(TextUtils.isEmpty(name)|TextUtils.isEmpty(pwd)){
Toast.makeText(this, "用户名或者密码不能为空", Toast.LENGTH_SHORT).show();
}else{
if(cb.isChecked()){
//记住密码(把用户名存储起来,应用程序只有在自己的空间才有操作权限) 根目录 /
//把东西存起来,先要有一个文件目录,在创建文件(createNewFile()),获得写入流(FileWriter),写入内容,关闭流
File file = new File(getFilesDir(),"itcast.txt");
FileWriter fw = null;
try {
file.createNewFile();
fw = new FileWriter(file);
fw.write(name+"\n");
fw.write(pwd);
fw.close();
Toast.makeText(this, "用户名保存成功", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "用户名保存失败", Toast.LENGTH_SHORT).show();
}
回显数据
1.文件存的地址 2.判断是否存在,存在 BufferedReader br = new BufferedReader(new FileReader(file)); String name = br.readLine(); et_name.setText(name); et_name.setSelection(name.length());
//回显数据 去读取文件内容到界面中 File file = new File(getFilesDir(),"itcast.txt"); if(file.exists()){ BufferedReader br = null; try { br = new BufferedReader(new FileReader(file)); String name = br.readLine(); String pwd = br.readLine(); et_name.setText(name); et_pwd.setText(pwd); et_name.setSelection(name.length()); } catch (Exception e) { e.printStackTrace(); } }