将文件保存到sd卡中,并且具备读取和清除功能,可以当作一个工具类来用。
public class SpUtils {
/**
* 保存文本
*
* @param context
* @param username
* @return
*/
public static boolean saveUserInfo(Context context, String bluetooth) {
try {
// 使用Android上下文获取当前项目的路径
File file = new File(context.getFilesDir(), "bluetooth.txt");
// 创建输出流对象
FileOutputStream fos = new FileOutputStream(file);
// 向文件中写入信息
fos.write((bluetooth).getBytes());
// 关闭输出流对象
fos.close();
return true;
} catch (Exception e) {
throw new RuntimeException();
}
}
* 保存文本
*
* @param context
* @param username
* @return
*/
public static boolean saveUserInfo(Context context, String bluetooth) {
try {
// 使用Android上下文获取当前项目的路径
File file = new File(context.getFilesDir(), "bluetooth.txt");
// 创建输出流对象
FileOutputStream fos = new FileOutputStream(file);
// 向文件中写入信息
fos.write((bluetooth).getBytes());
// 关闭输出流对象
fos.close();
return true;
} catch (Exception e) {
throw new RuntimeException();
}
}
/***
*获取路径下的内容
*/
public static String getTxtFileInfo(Context context) {
try {
// 创建FIle对象
File file = new File(context.getFilesDir(), "bluetooth.txt");
// 创建FileInputStream对象
FileInputStream fis = new FileInputStream(file);
// 创建BufferedReader对象
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
// 获取文件中的内容
String content = br.readLine();
try {
// 创建FIle对象
File file = new File(context.getFilesDir(), "bluetooth.txt");
// 创建FileInputStream对象
FileInputStream fis = new FileInputStream(file);
// 创建BufferedReader对象
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
// 获取文件中的内容
String content = br.readLine();
// 关闭流对象
fis.close();
br.close();
return content;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
fis.close();
br.close();
return content;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 清除本应用内部缓存(/data/data/com.xxx.xxx/cache)
*
* @param context
*/
public static void cleanInternalCache(Context context) {
File file = new File(context.getFilesDir(), "bluetooth.txt");
file.delete();
}
* 清除本应用内部缓存(/data/data/com.xxx.xxx/cache)
*
* @param context
*/
public static void cleanInternalCache(Context context) {
File file = new File(context.getFilesDir(), "bluetooth.txt");
file.delete();
}
}