android保存文件到sd卡,读取和清空记录功能(可以用来保存用户名和密码)

将文件保存到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();
 }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值