CSV是一种逗号分隔值格式的文件,一般用来存储数据的纯文本格式文件。Java对象转CSV,有现成的工具包,commons-lang3的ReflectionToStringBuilder就可以简单的解决的对象转CSV,快跟随小编一起学习一下吧
介绍
csv全称“Comma-Separated Values”,是一种逗号分隔值格式的文件,是一种用来存储数据的纯文本格式文件。CSV文件由任意数目的记录组成,记录间以某种换行符分隔;每条记录由字段组成,字段间的分隔符是其它字符或字符串。
Java对象转CSV,有现成的工具包,commons-lang3 的ReflectionToStringBuilder 就可以简单的解决的对象转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);
}
}
到此这篇关于Java实现对象转CSV格式的文章就介绍到这了,更多相关Java对象转CSV内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
本文标题为:Java实现对象转CSV格式
基础教程推荐
- Java实现线程插队的示例代码 2022-09-03
- java基础知识之FileInputStream流的使用 2023-08-11
- JDK数组阻塞队列源码深入分析总结 2023-04-18
- Java数据结构之对象比较详解 2023-03-07
- Java实现查找文件和替换文件内容 2023-04-06
- java实现多人聊天系统 2023-05-19
- Java文件管理操作的知识点整理 2023-05-19
- Java并发编程进阶之线程控制篇 2023-03-07
- springboot自定义starter方法及注解实例 2023-03-31
- ConditionalOnProperty配置swagger不生效问题及解决 2023-01-02