zoukankan      html  css  js  c++  java
  • [java] java解析txt文件

    /**
         * 读取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("	");
                        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;
            }
        }
    

      

  • 相关阅读:
    全球疫情可视化
    ListView(1)
    《浪潮之巅》阅读笔记02
    Intern Day12
    Intern Day11
    Intern Day10
    Intern Day10
    Intern Day10
    PTA1065
    Intern Day10
  • 原文地址:https://www.cnblogs.com/lonelywolfmoutain/p/6042629.html
Copyright © 2011-2022 走看看