一、寫文件
<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");