/** * 读取txt文件内容封装为map返回 * @param filePath * @return */ public static String readTxt(String filePath){ List<Map<String,Object>> lists= Lists.newArrayList(); try { File file=new File(filePath); InputStreamReader read = new InputStreamReader( new FileInputStream(file),ENCODING);//考虑到编码格式 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while((lineTxt = bufferedReader.readLine()) != null){ String[] keys= lineTxt.split("\t"); Map<String,Object> map = Maps.newHashMap(); for(String key:keys){ String[] values =key.split("="); if(!StringUtils.isEmpty(values[0])){ map.put(values[0],values[1]); } } lists.add(map); } read.close(); return JSONArray.toJSONString(lists); } catch (Exception e) { log.error(e.getMessage(),e); return null; } }
时间: 2024-11-05 11:39:09