1.文件输入输出流
1.1写文件到手机内存
public static boolean saveUserInfo(Context context, String number, String password) {
try {
// String path = "/data/data/com.itheima28.qqlogin/itheima28.txt";
// File filesDir = context.getFilesDir();
File filesDir = context.getCacheDir();
File f = new File(filesDir, "itheima28.txt");
FileOutputStream fos = new FileOutputStream(f);
// 307966990##123123
String data = number + "##" + password;
fos.write(data.getBytes());
fos.flush();
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
1.2从手机内存读文件
public static Map<String, String> getUserInfo(Context context) {
try {
// String path = "/data/data/com.itheima28.qqlogin/itheima28.txt";
// File filesDir = context.getFilesDir();
File filesDir = context.getCacheDir();
File f = new File(filesDir, "itheima28.txt");
FileInputStream fis = new FileInputStream(f);
// 字符流对象
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
// 307966990##123123
String text = reader.readLine();
if(!TextUtils.isEmpty(text)) {
String[] split = text.split("##");
Map<String, String> userInfoMap = new HashMap<String, String>();
userInfoMap.put("number", split[0]);
userInfoMap.put("password", split[1]);
return userInfoMap;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
1.3写文件到SD卡
public static boolean saveUserInfo(Context context, String number, String password) {
try {
// 判断当前的手机是否有sd卡
String state = Environment.getExternalStorageState();
if(!Environment.MEDIA_MOUNTED.equals(state)) {
// 已经挂载了sd卡
return false;
}
File sdCardFile = Environment.getExternalStorageDirectory();
File file = new File(sdCardFile, "itheima28.txt");
FileOutputStream fos = new FileOutputStream(file);
String data = number + "##" + password;
fos.write(data.getBytes());
fos.flush();
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
1.4从SD卡读文件
public static Map<String, String> getUserInfo(Context context) {
try {
// 判断当前的手机是否有sd卡
String state = Environment.getExternalStorageState();
if(!Environment.MEDIA_MOUNTED.equals(state)) {
// 已经挂载了sd卡
return null;
}
File sdCardFile = Environment.getExternalStorageDirectory();
File file = new File(sdCardFile, "itheima28.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String text = br.readLine();
br.close();
if(!TextUtils.isEmpty(text)) {
Map<String, String> userInfoMap = new HashMap<String, String>();
String[] split = text.split("##");
userInfoMap.put("number", split[0]);
userInfoMap.put("password", split[1]);
return userInfoMap;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
2.SharedPreferences存取文件
2.1存文件
public static boolean saveUserInfo(Context context, String number, String password) {
try {
// /data/data/包名/shared_prefs/itheima28
SharedPreferences sp = context.getSharedPreferences("itheima28", Context.MODE_PRIVATE);
// 获得一个编辑对象
Editor edit = sp.edit();
// 存数据
edit.putString("number", number);
edit.putString("password", password);
// 提交, 数据真正存储起来了.
edit.commit();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
2.2 取文件
public static Map<String, String> getUserInfo(Context context) {
SharedPreferences sp = context.getSharedPreferences("itheima28", Context.MODE_PRIVATE);
String number = sp.getString("number", null);
String password = sp.getString("password", null);
if(!TextUtils.isEmpty(number) && !TextUtils.isEmpty(password)) {
Map<String, String> userInfoMap = new HashMap<String, String>();
userInfoMap.put("number", number);
userInfoMap.put("password", password);
return userInfoMap;
}
return null;
}