Android文件缓存的简单应用Acache
16lz
2021-01-24
原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);// } }
更多相关文章
- Android(安卓)-HandleThread-给线程传参数
- 6.2、Android中向Internet发送xml数据
- Android(安卓)添加程序到桌面favorites列表
- Android(安卓)4.4以上使用HttpURLConnection底层使用OkHttp实现
- Android(安卓)常见的几种加密方式RSA、CBC的集成
- android 百度地图定位功能实现
- Android之WebViewClient与WebChromeClient的区别
- Android中使用OrmLite来对SQLite数据库进行CRUD(增删改查)解析
- Android(安卓)Intent Action 大全(转)