Loading...
墨滴

李泽涛。

2021/11/23  阅读:34  主题:极客黑

CSV读写、追加操作

CSV读写、追加操作

概述

  • 在现在的项目中,财务人员反馈,我要把6万条数据导出来生成excel直接报60s超时异常。这时候苦逼的程序猿就要开始查询问题了,发现我们数据放入excel的时候特别的慢,于是我们改用csv格式导出.

直接上代码

package com.eam.util;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.jxstar.util.MapUtil;
import org.jxstar.util.StringFormat;
import org.jxstar.util.StringUtil;

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * csv文件 操作工具类(写,追加)
 * @author lzt
 * editUser:楼梯间的男孩
 * editdate:2021/11/23
 *
 */

public class CsvFilePrinterTest {

    public static void main(String[] args) {
        //字段名
        List<String> titleList = new ArrayList<>();
        titleList.add("姓名");
        titleList.add("性别");
        titleList.add("语文");
        titleList.add("数学");
        //数据
        List<Map<String,String>> contextList = new ArrayList<>();
        Map<String,String> contextMap = new HashMap<>();
        contextMap.put("name","张三");
        contextMap.put("sex","女");
        contextMap.put("chinese","83");
        contextMap.put("math","95");
        contextList.add(contextMap);
        //绝对路径
        String absFilePath = "E:/test.csv";
        //创建scv文件并插入一行数据
        doExportCsv(titleList,contextList,absFilePath);
        //在往里面追加一条数据
        doAppendCsv(titleList,contextList,absFilePath);
    }

    /** ===========================================================
     *
     *    Purpose      :   往csv格式文件写内容
     *    @params:  titleList  csv格式头
     *    @params:  contextList 需要导出的对象集合
     *    @params:  absFilePath 文件绝对路径
     *    @return
     *    Author       :   lzt
     *    Created Date :   2021-11-23
     *    Update History
     *    Version       Date               Name            Description
     *    --------  ---------------   --------------  --------------------
     *     V1.0       2021-11-23           lzt            Creation
     * ===========================================================*/

    public static void doExportCsv(List<String> titleList, List<Map<String,String>> contextList, String absFilePath){
        FileOutputStream fileOutputStream = null;
        OutputStreamWriter outputStreamWriter = null;
        try{
            //获取文件流
            fileOutputStream = new FileOutputStream(absFilePath);
            outputStreamWriter = new OutputStreamWriter(fileOutputStream,"UTF-8");
            //第一行 头字段  姓名、性别、语文、数学
            String[] header = new String[titleList.size()];
            for(int i = 0;i<titleList.size();i++){
                String title = titleList.get(i);
                header[i] = title;
            }
            //默认头字段
            CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader(header);
            CSVPrinter csvPrinter = new CSVPrinter(outputStreamWriter,csvFormat);
            for(Map<String,String> contextMap:contextList){
                List<String> context = new ArrayList<>();
                for(String title:titleList){
                    String fileKey = "";
                    switch (title){
                        case "姓名" :
                            fileKey = "name";
                            break;
                        case "性别" :
                            fileKey = "sex";
                            break;
                        case "语文" :
                            fileKey = "chinese";
                            break;
                        case "数学" :
                            fileKey = "math";
                            break;
                    }
                    context.add(contextMap.get(fileKey));
                }
                //插入一行
                csvPrinter.printRecord(context);
            }
            csvPrinter.flush();
            csvPrinter.close();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(outputStreamWriter!=null){
                    outputStreamWriter.close();
                }
                if(fileOutputStream!=null){
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    /** ===========================================================
     *
     *    Purpose      :   往csv格式文件追加内容
     *    @params:  titleList  csv格式头
     *    @params:  contextList 需要导出的对象集合
     *    @params:  absFilePath 文件绝对路径
     *    @return
     *    Author       :   lzt
     *    Created Date :   2021-11-23
     *    Update History
     *    Version       Date               Name            Description
     *    --------  ---------------   --------------  --------------------
     *     V1.0       2021-11-23           lzt            Creation
     * ===========================================================*/

    public static void doAppendCsv(List<String> titleList, List<Map<String,String>> contextList, String absFilePath){
        FileWriter writer = null;
        try{
            //允许追加
            writer = new FileWriter(absFilePath, true);
            String[] header = new String[titleList.size()];
            for(int i = 0;i<titleList.size();i++){
                String title = titleList.get(i);
                header[i] = title;
            }
            //默认第一行是头字段
            CSVFormat csvFormat = CSVFormat.DEFAULT.withFirstRecordAsHeader();
            CSVPrinter csvPrinter = new CSVPrinter(writer,csvFormat);
            for(Map<String,String> contextMap:contextList){
                List<String> context = new ArrayList<>();
                for(String title:titleList){
                    String fileKey = "";
                    switch (title){
                        case "姓名" :
                            fileKey = "name";
                            break;
                        case "性别" :
                            fileKey = "sex";
                            break;
                        case "语文" :
                            fileKey = "chinese";
                            break;
                        case "数学" :
                            fileKey = "math";
                            break;
                    }
                    context.add(contextMap.get(fileKey));
                }
                //插入一行
                csvPrinter.printRecord(context);
            }
            csvPrinter.flush();
            csvPrinter.close();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(writer!=null){
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

效果

结论

  • 相对于excel导入来说 csv真的是大数据量下载的福音
  • csv生成非常容易,消耗的内存比excel少
  • 操作简单,消耗内存少,速度快,对于编程人员来讲,真香!
  • 最后,关注下无名的公众号吧【楼梯间的男孩】!

李泽涛。

2021/11/23  阅读:34  主题:极客黑

作者介绍

李泽涛。