一、写文件
<span style="white-space:pre"> </span>//data/data/com.xxx:这就是内部存储空间的路径
File file = new File("data/data/com.xxx/info.txt");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
fos.write((name + "##" + pass).getBytes());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
二、读文件
File file = new File("data/data/com.itheima.rwinrom/info.txt");
if(file.exists()){
try {
FileInputStream fis = new FileInputStream(file);
//把字节流转换成字符流
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
//读取txt文件里的用户名和密码
String text = br.readLine();
String[] s = text.split("##");
et_name.setText(s[0]);
et_pass.setText(s[1]);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
三、通过api来获得文件路径
//返回一个File对象,其路径是data/data/com.xxx/files
// File file = new File(getFilesDir(), "info.txt");
//返回值也是一个File对象,其路径是data/data/com.xxx/cache
File file = new File(getCacheDir(), "info.txt");