• 如何把csv文件转换为json格式


    public class CSVToJSon {
     
        private List<String> stringToList(String s, String sep) {
            if (s == null) {
                return null;
            }
     
            String[] parts = s.split(sep);
            return Arrays.asList(parts);
        }
     
        private String stringToJson(List<String> header, List<String> lineData) throws Exception {
     
            if (header == null || lineData == null) {
                throw new Exception("输入不能为null。");
            } else if (header.size() != lineData.size()) {
                throw new Exception("表头个数和数据列个数不等。");
            }
     
            StringBuilder sBuilder = new StringBuilder();
            sBuilder.append("{ ");
            for (int i = 0; i < header.size(); i++) {
                String mString = String.format(""%s":"%s"", header.get(i), lineData.get(i));
                sBuilder.append(mString);
     
                if (i != header.size() - 1) {
                    sBuilder.append(", ");
                }
            }
            sBuilder.append(" }");
     
            return sBuilder.toString();
        }
     
        public void ConvertToJson(InputStream filePath, OutputStream outPutPath) throws Exception {
           InputStreamReader isr=new InputStreamReader(filePath,"utf-8");

      BufferedReader reader=new BufferedReader(isr);

      OutputStreamWriter osw=new OutputStreamWriter(outPutPath,"utf-8");

      BufferedWriter reader=new BufferedWriter(osw);


            try {
                String sep = ",";
                String headerStr = reader.readLine();
     
                if (headerStr.trim().isEmpty()) {
                    System.out.println("表格头不能为空");
                    return;
                }
                
                List<String> header = stringToList(headerStr, sep);
                String line;
                int lineCnt = 1;
                while ((line = reader.readLine()) != null) {
     
                    if (line.trim().isEmpty()) {
                        System.out.println("第" + lineCnt + "行为空,已跳过");
                        continue;
                    }
                    
                    List<String> lineData = stringToList(line, sep);
     
                    if (lineData.size() != header.size()) {
                        String mString = String.format("第%d行数据列和表头列个数不一致 %s", lineCnt, line);
                        System.err.println(mString);
                        break;
                    }
     
                    String jsonStr = stringToJson(header, lineData);
                    writer.write(jsonStr);
                    writer.write(" ");
     
                    lineCnt++;
                }
            } finally {
                if (reader != null) {
                    reader.close();
                }
                if (writer != null) {
                    writer.close();
                }
            }
        }
     
        public static void main(String[] args) throws Exception {
            // TODO Auto-generated method stub
            InputStream filePath=new FileInputStream("路径");
       OutputStream outPutPath=new OutputStream("路径");
            CSVToJSon csvToJSon = new CSVToJSon();
            csvToJSon.ConvertToJson(filePath, outPutPath);
            System.out.println("处理完成。");
        }
     
    }

  • 相关阅读:
    人这一辈子
    理性不是逆来顺受
    旧瓶新酒:江城子
    HVAC专业相关网站
    韩寒:主子,奴才和狗
    百无一用是书生
    inove主题文章字体修改
    这个世界清净了:再见人人
    ActiveX控件开发(转)
    GIS大讲堂内所有讲座的索引(更新至2008年6月26日)(转)
  • 原文地址:https://www.cnblogs.com/hhthtt/p/10931340.html
Copyright © 2020-2023  润新知