Java-对象转CSV

x33g5p2x  于2022-06-30 转载在 Java  
字(7.7k)|赞(0)|评价(0)|浏览(419)

介绍

csv全称“Comma-Separated Values”,是一种逗号分隔值格式的文件,是一种用来存储数据的纯文本格式文件。CSV文件由任意数目的记录组成,记录间以某种换行符分隔;每条记录由字段组成,字段间的分隔符是其它字符或字符串。

Java对象转CSV,有现成的工具包,commons-lang3ReflectionToStringBuilder 就可以简单的解决的对象转CSV 但是复杂点的处理还是不行,而且在运行速度上,我测试了下我自己写的,比这个快多了,1百万条数据大约也就2秒左右, 如果对速度有要求的话,我们可以使用多线程进行分割,比如1千万条数据,那么我开10个线程,1个线程执行100万条数据,所有线程执行完毕后在按照顺序拼接到一起,这样1千万条数据也就最多3~5秒执行完毕

代码样例

package com.file.csv;

import com.date.LocalDateUtils;
import lombok.Data;
import lombok.SneakyThrows;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

/**
 * 对象转csv格式
 *
 * @describe
 * 1百万数据测试 2~3秒
 * 调用顺序如下(根据情况自行选择)
 *         String s = ObjToCSV.create(userDatas)
 *                 .fieldsAll()   //转换全部列
 *                 .addIncludeFields() //只包含某些列
 *                 .addExcludeFields("roleData") //排除某些列
 *                 .addHead() //添加头部字段
 *                 .addContent() //一个对象(一行)
 *                 .addContents() //多个对象(多行)
 *                 .addEnding((data)->{ //结尾说明
 *                     JSONObject jsonObject = new JSONObject();
 *                     jsonObject.put("count",data.getNumber());
 *                     jsonObject.put("time",data.getCreateDate());
 *                     return  jsonObject.toJSONString();
 *                 })
 *                 .ToString(); //结果
 */
@Data
public class ObjToCSV<T> {

    private String separator = "\u0001|\u0001";  //默认分隔符
    private StringBuilder str = new StringBuilder();
    private T object;
    private List<T> objects;
    private List<String> excludeFields = new ArrayList<String>(100) {{
        add("serialVersionUID");
    }};
    private List<String> includeFields = new ArrayList<String>(100);
    private boolean fieldsAll = false;
    private  int number;//多少行一共
    //日期串 20220617204504
    private  String createDate=LocalDateUtils.getLocalDateTimeStr1() ;

    private ObjToCSV(T object, String separator) {
        this.object = object;
        this.separator = separator;
        //行数
        this.number=1;
    }
    private ObjToCSV(T object) {
        this.object = object;
        //行数
        this.number=1;

    }
    private ObjToCSV(List<T> objects) {
        if (objects.isEmpty()) {
            throw new NullPointerException("不能是空");
        }
        this.object = objects.get(0);
        this.objects = objects;
        //行数
        this.number=objects.size();
    }
    private ObjToCSV(List<T> objects, String separator) {
        if (objects.isEmpty()) {
            throw new NullPointerException("不能是空");
        }
        this.object = objects.get(0);
        this.objects = objects;
        this.separator = separator;
        //行数
        this.number=objects.size();

    }

    // 排除和包含都有,那么以包含的为主
    private boolean decideFields(String fieldName) {

        //包含
        if (includeFields.contains(fieldName)) {
            return false;
        }
        //排除
        if (excludeFields.contains(fieldName)) {
            return true;
        }

        // 开启全部放行
        if (fieldsAll) {
            return false;
        }

        //默认拦截全部
        return true;
    }

    public static <T> ObjToCSV<T> create(T object, String separator) {
        return new ObjToCSV<T>(object, separator);
    }
    public static <T> ObjToCSV<T> create(T object) {
        return new ObjToCSV<T>(object);
    }
    public static <T> ObjToCSV<T> create(List<T> object, String separator) {
        return new ObjToCSV<T>(object, separator);
    }
    public static <T> ObjToCSV<T> create(List<T> object) {
        return new ObjToCSV<T>(object);
    }
    //全部放行
    public ObjToCSV<T> fieldsAll() {
        fieldsAll = true;
        return this;
    }

    //包含
    public ObjToCSV<T> addIncludeFields(String... fieldName) {
        includeFields.addAll(Arrays.asList(fieldName));
        return this;
    }

    //排除
    public ObjToCSV<T> addExcludeFields(String... fieldName) {
        excludeFields.addAll(Arrays.asList(fieldName));
        return this;
    }

    //添加头部
    public ObjToCSV<T> addHead() {
        StringBuilder str1 = new StringBuilder();
        Class<?> aClass = this.object.getClass();
        Field[] fields = aClass.getDeclaredFields();
        int length = fields.length;
        for (int i = 0; i < length; i++) {
            Field field = fields[i];
            field.setAccessible(true);
            if (decideFields(field.getName())) {
                continue;
            }
            str1.append(field.getName()).append(separator);
        }
        int length1 = separator.length();
        String substring = str1.substring(0, str1.length() - length1);
        this.str.append(substring + "\n");
        return this;
    }
    //在结尾补充自定义的描述
    public ObjToCSV<T> addEnding(Function<ObjToCSV,String> functor){
        String apply = functor.apply( this);
        this.str.append(apply);
        return this;
    }

    //将对象转换为CSV格式字符串
    @SneakyThrows
    public ObjToCSV<T> addContent() {
        StringBuilder str1 = new StringBuilder();
        Class<?> aClass = this.object.getClass();
        Field[] fields = aClass.getDeclaredFields();
        int length = fields.length;
        for (int i = 0; i < length; i++) {
            Field field = fields[i];
            field.setAccessible(true);
            if (decideFields(field.getName())) {
                continue;
            }
            Object o = field.get(object);
            String value = "";
            if (o != null) {
                value = String.valueOf(o);
            }
            str1.append(value).append(separator);
        }
        int length1 = separator.length();
        String substring = str1.substring(0, str1.length() - length1);
        this.str.append(substring + "\n");

        return this;
    }
    //将多个对象转换为csv格式
    public ObjToCSV<T> addContents() {
        for (T t : this.objects) {
            this.object=t;
            addContent();
        }
        return this;
    }
    //将结果输出为字符串
    public String ToString() {
        return str.toString();
    }


}

测试样例

package com.file;

import com.alibaba.fastjson.JSONObject;
import com.data.RandomUserData;
import com.entity.UserData;
import com.file.csv.ObjToCSV;
import com.file.csv.ObjToCSV;
import com.function.impl.CodeStartAndStopTimeUtil;
import lombok.SneakyThrows;
import org.junit.Test;

import java.util.List;

public class ObjToCSVTest {

    //单个对象
    @Test
    public  void show1(){
        UserData userOne = RandomUserData.getUserOne();
        String s = ObjToCSV.create(userOne)
                .fieldsAll()
//                .addIncludeFields()
                .addExcludeFields("roleData")
//                .addHead()
                .addContent()
//                .addContents()
                .addEnding((data)->{
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("count",data.getNumber());
                    jsonObject.put("time",data.getCreateDate());
                    jsonObject.put("separator",data.getSeparator());
                    return  jsonObject.toJSONString();
                })
                .ToString();
        System.out.println(s);

    }

    //多个对象,可以使用自定义分隔符
    @Test
    public  void show2(){
        UserData userOne = RandomUserData.getUserOne();
        String s = ObjToCSV.create(userOne,"|")
                .fieldsAll()
                .addExcludeFields("roleData")
                .addContent()
                .addEnding((data)->{
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("count",data.getNumber());
                    jsonObject.put("time",data.getCreateDate());
                    jsonObject.put("separator",data.getSeparator());
                    return  jsonObject.toJSONString();
                })
                .ToString();
        System.out.println(s);
    }


    //排除不显示的,其他都显示
    @Test
    public  void show3(){
        List<UserData> userDatas = RandomUserData.getUserDatas(10);

        String s = ObjToCSV.create(userDatas,"|")
                .fieldsAll()//显示全部字段
                .addExcludeFields("roleData")//排除指定字段
                .addHead()
                .addContents()
                .addEnding((data)->{
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("count",data.getNumber());
                    jsonObject.put("time",data.getCreateDate());
                    jsonObject.put("separator",data.getSeparator());

                    return  jsonObject.toJSONString();
                })
                .ToString();
        System.out.println(s);
    }

    //只包含指定字段 ,其他都不显示
    @Test
    public  void show4(){
        List<UserData> userDatas = RandomUserData.getUserDatas(10);

        String s = ObjToCSV.create(userDatas,"|")
                .addIncludeFields("id","name")
                .addHead()  //添加头部
                .addContents() //添加内容
                .addEnding((data)->{  //添加结尾信息
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("count",data.getNumber());
                    jsonObject.put("time",data.getCreateDate());
                    jsonObject.put("separator",data.getSeparator());
                    return  jsonObject.toJSONString();
                })
                .ToString();
        System.out.println(s);
    }

    //内容
    @Test
    public  void show5(){
        List<UserData> userDatas = RandomUserData.getUserDatas(10);
        String s = ObjToCSV.create(userDatas,"|")
                .fieldsAll()
                .addExcludeFields("roleData")//排除指定字段
                .addContents() //添加内容
                .ToString();
        System.out.println(s);
    }

    @SneakyThrows
    public static void main(String[] args) {
        List<UserData> userDatas = RandomUserData.getUserDatas(100);
        String s = ObjToCSV.create(userDatas)
                .fieldsAll()
//                .addIncludeFields()
                .addExcludeFields("roleData")
//                .addHead()
//                .addContent()
                .addContents()
                .addEnding((data)->{
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("count",data.getNumber());
                    jsonObject.put("time",data.getCreateDate());
                    return  jsonObject.toJSONString();
                })
                .ToString();
        System.out.println(s);

    }
}

点赞 -收藏-关注-便于以后复习和收到最新内容有其他问题在评论区讨论-或者私信我-收到会在第一时间回复如有侵权,请私信联系我感谢,配合,希望我的努力对你有帮助^_^

相关文章

最新文章

更多