android保存文件到sd卡,读取和清空记录功能(可以用来保存用户名和密码)
16lz
2021-01-26
将文件保存到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();
}
} /*** *获取路径下的内容 */
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();
// 关闭流对象
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();
}
}
更多相关文章
- android 截屏的三种方法
- Android(安卓)存储路径选择方法
- Android(安卓)布局转化为View对象的两种方法
- 使用Adobe Flash Builder Burrito创建AIR For Google Android应
- android 里面的 Drawable 和 ConstantState
- Android(安卓)-- 超全的 File,Bitmap,Drawable,Uri, FilePath ,byte
- android红米等关于读取本地文件夹图片获取路径的问题的解决
- android中打开本地doc、pdf、ppt、text等文件功能的实现
- Android(安卓)adb方式获取手机总内存和可用内存信息