Android存储Json到本地,和读取本地Json
/** * 保存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(); }