近期做指纹识别,需要用到缓存文件,数据量并不大,用redis不合适,所以用到了txt文件。

思路是 1、定时查询指纹,存到txt缓存文件中。

       2、新增或删除指纹时,查询指纹,存到txt缓存文件中。

    3、需要对比查询指纹时,从txt缓存文件中查找,若缓存文件为空,则从数据库查找。

实现后,速度从9S提升到了最快0.7S。

期间用到了 List<Map<String, Object>> 转为 json 存到txt文件中,txt 文件中的 json 内容转为 List<Map<String, Object>> 。

转换方式如下:

1、List<Map<String, Object>> 转为 json(String)

List<Map<String, Object>> list= openDao.queryForList(map);

String str = JSON.toJSONString(list); //此行转换

caChe.writeFile(finerPath,finerPath+"finerCaChe.txt",str);

writeFile 方法

/**
* 写入TXT文件
*/
public static void writeFile(String dirPath,String path,String txt) {
try {
judeDirExists(new File(dirPath)); File writeName = new File(path); // 相对路径,如果没有则要建立一个新的output.txt文件
writeName.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖
try (FileWriter writer = new FileWriter(writeName);
BufferedWriter out = new BufferedWriter(writer)
) {
out.write(txt); // \r\n即为换行
out.flush(); // 把缓存区内容压入文件
}
} catch (IOException e) {
e.printStackTrace();
}
}

2、 json 转为 List<Map<String, Object>>

StringBuilder line = readFile(path); //读取txt文本内容
List< Map<String,Object>> listw = toListMap(line.toString()); //此行转换
toListMap方法
public static List<Map<String, Object>> toListMap(String json){
List<Object> list =JSON.parseArray(json); List< Map<String,Object>> listw = new ArrayList<Map<String,Object>>();
for (Object object : list){
Map<String,Object> ageMap = new HashMap<String,Object>();
Map <String,Object> ret = (Map<String, Object>) object;//取出list里面的值转为map
listw.add(ret);
}
return listw; }

readFile方法

/**
* 读入TXT文件
*/
public static StringBuilder readFile(String path) {
String pathname = path; // 绝对路径或相对路径都可以,写入文件时演示相对路径,读取以上路径的input.txt文件
//防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw;
//不关闭文件会导致资源的泄露,读写文件都同理
//Java7的try-with-resources可以优雅关闭文件,异常时自动关闭文件;详细解读https://stackoverflow.com/a/12665271 StringBuilder txt =new StringBuilder("");
try (FileReader reader = new FileReader(pathname);
BufferedReader br = new BufferedReader(reader) // 建立一个对象,它把文件内容转成计算机能读懂的语言
) {
String line;
while ((line = br.readLine()) != null) {
// 一次读入一行数据
txt.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return txt;
}

List<Map<String, Object>> 与 json 互转的更多相关文章

  1. javaBean与Map<String,Object>互转

    背景:有时候想不通阿帕奇的BeanUtils是怎么进行map和Bean互相转化的. 工作闲暇之余,自己写个一小段代码,一探究竟,试试才发现,原来一切并非我们想的那么什么和复杂. 注:这里只是简单实例, ...

  2. json遍历,List<Map<String,Object>>遍历

    js怎样给input对象追加属性,如disabled $(":textbox").attr({"disabled":true}); List<Map< ...

  3. fastjson 将json字符串转化成List<Map<String, Object>>

    亲测可行,如下: JSON.parseObject(jsonstr, new TypeReference<List<Map<String, Object>>>() ...

  4. 用第三方工具类,将JavaBean、List、Map<String,Object>转成JSON文本

    导入第三方jar包: >commons-beanutils-1.7.0.jar >commons-collections-3.1.jar >commons-lang-2.5.jar ...

  5. Map<String, Object>转Object,Object转 Map<String, Object>

    Map转Object import com.alibaba.fastjson.JSON; Map<String, Object> boneAgeOrderMap=boneAgeOrderS ...

  6. String, JSONArray , JSONObject ,Map<String, Object> 与对象

    String pic = "[{\"picServiceUrl\": \"0f4bb44afb2e48d48b786d3bbdeec283/20180408/6 ...

  7. String 转 List<Map<String, Object>>

    public static List<Map<String, Object>> toListMap(String json){ List<Object> list ...

  8. Map<String,Object>接收参数,Long类型降级为Integer,报类型转换异常

    前言 今天看群里小伙伴问了一个非常有意思的问题: 使用 Map<String,Object> 对象接收前端传递的参数,在后端取参时,因为接口文档中明确该字段类型为 Long ,所以对接收的 ...

  9. 分页查询和分页缓存查询,List<Map<String, Object>>遍历和Map遍历

    分页查询 String sql = "返回所有符合条件记录的待分页SQL语句"; int start = (page - 1) * limit + 1; int end = pag ...

随机推荐

  1. thinkphp Ajax表单提交

    ajax无刷新提示...局部刷新... http://www.thinkphp.cn/extend/230.html 保存表单数据的表 绿色的部分就是ajax显示出来的东西 控制器 ajax检查标题 ...

  2. selenium测试(Java)(三)

    控制浏览器: http://www.cnblogs.com/moonpool/p/5657752.html

  3. numpy库中的知识点——积累

    下面是一些杂碎的知识点: 首先我们说说多维数组: 数组的属性: ndarray.ndim, 表示数组的秩是多少: ndarray.shape,返回数组的形状: ndarray.size,数组元素的总个 ...

  4. 【Java面试题】58 char型变量中能不能存贮一个中文汉字?为什么?

    char型变量是用来存储Unicode编码的字符的,unicode编码字符集中包含了汉字,所以,char型变量中当然可以存储汉字啦.不过,如果某个特殊的汉字没有被包含在unicode编码字符集中,那么 ...

  5. Vuforia AR实战教程

    官网:https://developer.vuforia.com/ Vuforia AR实战教程 http://www.taikr.com/my/course/531. AQaVpF//////AAA ...

  6. Ubuntu 安装 Kubernetes

    Kubernetes是Google开源的容器集群管理系统.它构建于docker技术之上,为容器化的应用提供资源调度.部署运行.服务发现.扩容缩容等整一套功能,本质上可看作是基于容器技术的mini-Pa ...

  7. _mysql_exceptions.ProgrammingError:(2014, "commands out of sync; you can't run this command now")

    今天,测试dashboard上的一些graph, 发现,当多个graph同时向后台请求数据(异步)的时候, 出现了上述错误.而且,三个bug交替出现,另外两个bug分别是:python stop re ...

  8. mysql数据库binary log中的事件到底是什么?

    需求描述: 最近看mysql备份恢复的时候,基于时间点恢复,提到了binary log中存的是"事件" 那么到底什么是事件呢 概念解释: binary log中存的是事件(even ...

  9. mybatis由浅入深day02_7.3二级缓存

    7.3 二级缓存 7.3.1 原理 下图是多个sqlSession请求UserMapper的二级缓存图解. 首先开启mybatis的二级缓存. sqlSession1去查询用户id为1的用户信息,查询 ...

  10. HTML表单的应用

    <html> <head> <title>HTML表单的应用</title> </head> <body> <!-- bo ...