一、文件方式
1、保存数据到手机内存
/**
* 使用文件的方式保存信息到手机内存
*
* @param context
* @param fileName
* ,文件名,不包含路径
* @param info
* ,保存内容
* @return 成功返回true,失败返回false
*/
public static boolean saveInfoUseFileToMemory(Context context,
String fileName, String info) {
FileOutputStream fos = null;
boolean successFlag = false;
try {
// 如果该路径的文件不存在则会创建该文件
// 路径 /data/data/包名/files/fileName
fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
fos.write(info.getBytes());
successFlag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
if (successFlag)
return true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
2、从手机内存中获取数据
/**
* 使用文件的方式从手机内存获取保存的信息
*
* @param context
* @param fileName
* 文件名,不包含路径
* @return 异常返回null,成功返回字符串
*/
public static String getInfoUseFileToMemory(Context context, String fileName) {
StringBuilder sb = new StringBuilder();
FileInputStream fis = null;
BufferedReader reader = null;
boolean successFlag = false;
try {
// 路径 /data/data/包名/files/fileName
fis = context.openFileInput(fileName);
reader = new BufferedReader(new InputStreamReader(fis));
String str = null;
while ((str = reader.readLine()) != null) {
sb.append(str);
str = null;
}
successFlag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
if (successFlag)
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
3、保存数据到手机SD卡
/**
* 使用文件的方式保存信息到SD卡
*
* @param context
* @param fileName
* 文件名,可包含路径,例如 12beli/test.txt或者test.txt
* @param info
* @return 成功返回true,失败返回false
*/
public static boolean saveInfoUseFileToSDCart(Context context,
String fileName, String info) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
FileOutputStream fos = null;
boolean successFlag = false;
//获取SD卡的根路径的文件对象
File rootFile = Environment.getExternalStorageDirectory();
//把根路径的文件对象转为字符串的绝对路径
String rootStr = rootFile.getAbsolutePath();
String path = rootStr + "/" + fileName;
//保存数据的最终路径的文件对象
File f = new File(path);
/**
* 假如fileName就是12beli/test.txt,那么f.getParentFile()就是 SD卡根路径/12beli 这个路径对应
* 的文件对象,如果这个文件夹还不存在,那么就要创建该文件夹。
*
* mkdir()和mkdirs()的区别:
* 假如 file对象指向 /sdcart/aaa/bbb 这个路径,
* 那么file.mkdir()只会创建bbb这个文件夹,而且前提是bbb文件夹之前的所有路径都必须存在,否则异常。
* file.mkdirs()不管bbb文件夹之前的路径是否已经存在,都会把所有的文件夹创建。
*/
if (!f.getParentFile().exists()) {
f.getParentFile().mkdirs();
}
try {
// 路径 /sdcart/XX/test.txt
fos = new FileOutputStream(f);
fos.write(info.getBytes());
successFlag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
if (successFlag)
return true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return false;
}
4、从手机SD卡中获取数据
/**
* 使用文件的方式从SD卡获取信息
*
* @param context
* @param fileName
* 文件名,可包含路径。 例如12beli/test.txt或者test.txt
* @return 抛异常或者SD卡不存在返回null,成功返回字符串
*/
public static String getInfoUseFileToSDCart(Context context, String fileName) {
StringBuilder sb = new StringBuilder();
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
FileInputStream fis = null;
BufferedReader reader = null;
boolean successFlag = false;
File rootFile = Environment.getExternalStorageDirectory();
String rootStr = rootFile.getAbsolutePath();
String path = rootStr + "/" + fileName;
File f = new File(path);
try {
// 路径 /data/data/包名/files/fileName
fis = new FileInputStream(f);
reader = new BufferedReader(new InputStreamReader(fis));
String str = null;
while ((str = reader.readLine()) != null) {
sb.append(str);
str = null;
}
successFlag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
if (successFlag)
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return null;
}
5、获取SD卡总内存大小
/**
* 获取SD卡内存大小
*
* @return 返回单位MB
*/
@SuppressLint("NewApi")
public static long getSDCartTotalSize() {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File file = Environment.getExternalStorageDirectory();
StatFs statFs = new StatFs(file.getAbsolutePath());
long blockSize = statFs.getBlockSizeLong();
long blockCount = statFs.getBlockCountLong();
long total = (blockSize * blockCount) / 1024;
return total / 1024;
}
return 0;
}
6、获取SD卡可用内存大小
/**
* 获取SD卡可用内存大小
*
* @return 返回单位MB
*/
@SuppressLint("NewApi")
public static long getSDCartAvailableSize() {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File file = Environment.getExternalStorageDirectory();
StatFs statFs = new StatFs(file.getAbsolutePath());
long blockSize = statFs.getBlockSizeLong();
long availableCount = statFs.getAvailableBlocksLong();
long total = (blockSize * availableCount) / 1024;
return total / 1024;
}
return 0;
}
7、存储数据到应用程序缓存中
/**
* 使用文件的方式 存储数据到应用程序的缓存中
*
* @param context
* @param fileName
* :文件名,不包含路径
* @param info
* 需要保存的字符串
* @return 成功返回true,失败返回false
*/
public static boolean saveInfoUseFileToCache(Context context,
String fileName, String info) {
// 存储路径 /data/data/包名/cache/fileName
File cacheFile = context.getCacheDir();
File file = new File(cacheFile, fileName);
FileOutputStream fos = null;
boolean successFlag = false;
try {
fos = new FileOutputStream(file);
fos.write(info.getBytes());
successFlag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
if (successFlag)
return true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
8、读取应用程序缓存的数据
/**
* 使用文件的方式从应用程序缓存中获取数据
*
* @param context
* @param fileName
* 文件名,不包含路径
* @return 异常返回null,成功返回字符串
*/
public static String getInfoUseFileToCache(Context context, String fileName) {
StringBuilder sb = new StringBuilder();
File cacheFile = context.getCacheDir();
File file = new File(cacheFile, fileName);
FileInputStream fis = null;
BufferedReader reader = null;
boolean successFlag = false;
try {
// 路径 /data/data/包名/cache/fileName
fis = new FileInputStream(file);
reader = new BufferedReader(new InputStreamReader(fis));
String str = null;
while ((str = reader.readLine()) != null) {
sb.append(str);
str = null;
}
successFlag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
if (successFlag)
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
二、SharedPreference方式存储数据
1、存储数据
/**
* 使用SharedPreferences的方式存储信息到手机内存
* @param context
* @param fileName 文件名 不包含路径
* @param key 键
* @param value 保存的值,可以为boolean int long float String类型
* @return 保存成功返回true,失败返回false
*/
public static boolean saveInfoUseSharedPreferencesToMemory(Context context,
String fileName, String key, Object value) {
try {
//路径 /data/data/包名/shared_prefs/XX
SharedPreferences sp = context.getSharedPreferences(fileName,
Context.MODE_PRIVATE);
Editor editor = sp.edit();
if (value instanceof Boolean) {
editor.putBoolean(key, (Boolean) value);
} else if (value instanceof Integer) {
editor.putInt(key, (Integer) value);
} else if (value instanceof Long) {
editor.putLong(key, (Long) value);
} else if (value instanceof Float) {
editor.putFloat(key, (Float) value);
} else if (value instanceof String) {
editor.putString(key, (String) value);
}
editor.commit();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
2、获取数据
/**
* 使用SharedPreferences的方式从手机内容获取存储信息
* @param context
* @param fileName 文件名,不包含路径
* @param key 保存的键名
* @return 返回Object类型
*/
public static Object getInfoUseSharedPreferencesToMemory(Context context,
String fileName, String key) {
SharedPreferences sp = context.getSharedPreferences(fileName,
Context.MODE_PRIVATE);
Map<String, ?> all = sp.getAll();
return all.get(key);
}