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

}

更多相关文章

  1. android 截屏的三种方法
  2. Android(安卓)存储路径选择方法
  3. Android(安卓)布局转化为View对象的两种方法
  4. 使用Adobe Flash Builder Burrito创建AIR For Google Android应
  5. android 里面的 Drawable 和 ConstantState
  6. Android(安卓)-- 超全的 File,Bitmap,Drawable,Uri, FilePath ,byte
  7. android红米等关于读取本地文件夹图片获取路径的问题的解决
  8. android中打开本地doc、pdf、ppt、text等文件功能的实现
  9. Android(安卓)adb方式获取手机总内存和可用内存信息

随机推荐

  1. Rexsee API介绍:Android屏幕锁定,Keyguard
  2. 在Service中新开线程和直接新开线程的区
  3. ratingbar 的使用
  4. 针对 CoordinatorLayout 及 Behavior 的
  5. Android开机log分析
  6. Android(安卓)ListView理解之BaseAdapter
  7. android 关于程序升级问题
  8. Android开发环境配置教程
  9. android 4.0.1 webkit Event 事件处理过
  10. Android中处理崩溃异常