zoukankan      html  css  js  c++  java
  • 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();
        }
  • 相关阅读:
    powershell和cmd区别
    装饰器笔记
    url参数和字典的相互转化
    Python装饰器详解
    python字符串格式化f-string
    Python函数(function)与方法(method)区别
    jenkins钉钉插件报错keywords not in content
    jenkins配置邮件
    vim常用操作
    Vue之axios请求
  • 原文地址:https://www.cnblogs.com/niray/p/3953059.html
Copyright © 2011-2022 走看看