Android存储Json到本地,和读取本地Json
16lz
2021-01-23
/** * 保存json到本地 * @param mActivity * @param filename * @param content */ public static File dir = new File(Environment.getExternalStorageDirectory() + "/.Imageloader/json/"); public static void saveToSDCard(Activity mActivity, String filename, String content) { String en = Environment.getExternalStorageState(); //获取SDCard状态,如果SDCard插入了手机且为非写保护状态 if (en.equals(Environment.MEDIA_MOUNTED)) { try { dir.mkdirs(); //create folders where write files File file = new File(dir, filename); OutputStream out = new FileOutputStream(file); out.write(content.getBytes()); out.close(); AppUtils.showToast(mActivity, "保存成功"); } catch (Exception e) { e.printStackTrace(); AppUtils.showToast(mActivity, "保存失败"); } } else { //提示用户SDCard不存在或者为写保护状态 AppUtils.showToast(mActivity, "SDCard不存在或者为写保护状态"); } } /** * 从本地读取json * @param mActivity * @param filename * @param content */ public static String readTextFile(String filePath) { StringBuilder sb = new StringBuilder(); try { File file = new File(dir + "/" + filePath); InputStream in = null; in = new FileInputStream(file); int tempbyte; while ((tempbyte = in.read()) != -1) { sb.append((char) tempbyte); } in.close(); } catch (Exception e) { e.printStackTrace(); } return sb.toString(); }
更多相关文章
- Android 常用的提示框Dialog
- android判断网络的状态断开与否
- android 获取状态栏高度
- Android中怎么去掉手机的状态栏、标题栏
- Android 隐去标题栏 和 状态栏
- 自定义Toast,兼容那些安装时把通知关掉还问为啥不给提示的测试人
- Android 更新提示的AlertDialog
- Android——Activity去除标题栏和状态栏