本文共 1405 字,大约阅读时间需要 4 分钟。
private static boolean writeToTextFileByJson(List > datas, String title, String columns, String fileName, String encode) { final BufferedWriter bw; boolean flag = true; try { boolean hasExists = new File(fileName).exists(); if(!hasExists){ createLocDir(fileName);//创建目录 } File f = new File(fileName); bw = new BufferedWriter(new FileWriter(f, false)); final String[] allcolumns = columns.substring(0, columns.length()).split(","); Map resultMap = new HashMap (); String mobile = ""; List > columnList = new ArrayList >(); //mobileMap.put("mobile", (String) m.get(allcolumns[0].toUpperCase())); Object value =""; for(Map map : datas) { for (int i = 1; i < allcolumns.length; i++) { String columnName = allcolumns[i]; Set set = map.keySet(); if(set.contains(columnName)){ value = map.get(columnName); } mobile = (String) map.get(allcolumns[0].toUpperCase()); resultMap.put("mobile", mobile); HashMap columnMap = new HashMap (); columnMap.put("colum_name", columnName); columnMap.put("value", value); columnList.add(columnMap); resultMap.put("column", columnList); String json = JsonUtil.toJson(resultMap).toString(); bw.write(json); columnList.clear(); } } if (bw != null) { bw.close(); } } catch (Exception e) { flag = false; LOG.error("createFile(" + fileName + ") error:", e); e.printStackTrace(); } return flag; }
转载地址:http://rfgta.baihongyu.com/