原ASimpleCache GitHub地址:https://github.com/yangfuhai/ASimpleCache

1、使用时需要注意:readLine会清除换行符,且在大量换行时影响效率

 /**     * 读取 String数据     *     * @param key     * @return String 数据     */    public String getAsString(String key) {        //中文可能会乱码        File file = mCache.get(key);        if (!file.exists())            return null;        boolean removeFile = false;        BufferedInputStream bis = null;        try {            FileInputStream fis = null;            fis = new FileInputStream(file);            bis = new BufferedInputStream(fis);            String content = "";            //自己定义一个缓冲区            byte[] buffer = new byte[1024 * 8];            int flag = 0;            while ((flag = bis.read(buffer)) != -1) {                content += new String(buffer, 0, flag);            }            if (!Utils.isDue(content)) {                return Utils.clearDateInfo(content);            } else {                removeFile = true;                return null;            }        } catch (Exception e) {            e.printStackTrace();        } finally {            if (removeFile) {                remove(key);            }            try {                if (bis != null) {                    bis.close();                }            } catch (IOException e) {                e.printStackTrace();            }        }        return null;//        大量换行数据会很耗时,且换行符会丢失//        File file = mCache.get(key);//        if (!file.exists())//            return null;//        boolean removeFile = false;//        BufferedReader in = null;//        try {//            in = new BufferedReader(new FileReader(file));//            String readString = "";//            String currentLine;//            while ((currentLine = in.readLine()) != null) {//                readString += currentLine;//            }//            if (!Utils.isDue(readString)) {//                return Utils.clearDateInfo(readString);//            } else {//                removeFile = true;//                return null;//            }//        } catch (IOException e) {//            e.printStackTrace();//            return null;//        } finally {//            if (in != null) {//                try {//                    in.close();//                } catch (IOException e) {//                    e.printStackTrace();//                }//            }//            if (removeFile)//                remove(key);//        }    }

更多相关文章

  1. Android(安卓)-HandleThread-给线程传参数
  2. 6.2、Android中向Internet发送xml数据
  3. Android(安卓)添加程序到桌面favorites列表
  4. Android(安卓)4.4以上使用HttpURLConnection底层使用OkHttp实现
  5. Android(安卓)常见的几种加密方式RSA、CBC的集成
  6. android 百度地图定位功能实现
  7. Android之WebViewClient与WebChromeClient的区别
  8. Android中使用OrmLite来对SQLite数据库进行CRUD(增删改查)解析
  9. Android(安卓)Intent Action 大全(转)

随机推荐

  1. Android移植的Box2D
  2. android 霓虹灯效果
  3. 谷歌Android操作系统有望主导移动市场
  4. 简单解释Android中的任务、进程和线程
  5. android中使用AES加解密
  6. Android+SpringMVC通信
  7. Android(安卓)studio璁剧疆宸ヤ綔绌洪棿/
  8. Android主题定制及修改
  9. [android]笔记4-线性布局
  10. 熟悉Android打包编译的流程