java 11 openCSV 只写入文件特定的 bean 属性
java 11 openCSV write to file only specific bean properties
如何仅将特定的 bean 属性写入 csv 文件(使用 openCSV)
即给定 class
public class MyEntry {
private String propOne;
private Long propTwo;
private int propThree;
private String propFour;
private String propFive;
...
private String propFifteen;
//getters and setter omitted for brevity
}
可能存在我需要创建一个包含从 propFour
到 propTen
字段的 csv 文件的情况,在其他情况下,可能需要创建一个包含所有 bean 属性的 csv。
可能有很多情况需要将不同的字段导出到 csv 文件。如何在不为不同情况创建自定义 classes 的情况下实现它?
您可以创建 CSVWriter.java 并使用反射概念读取您的 class 的属性。请记住调用 setAccessible(true) 以便从外部访问私有字段
虚拟对象
import java.util.List;
public class Dummy {
public static final List<String> GROUP_1 = List.of("prop1", "prop2");
public static final List<String> GROUP_2 = List.of("prop1", "prop3");
private String prop1;
private Integer prop2;
private String prop3;
public String getProp1() {
return prop1;
}
public Integer getProp2() {
return prop2;
}
public String getProp3() {
return prop3;
}
public void setProp1(String prop1) {
this.prop1 = prop1;
}
public void setProp2(Integer prop2) {
this.prop2 = prop2;
}
public void setProp3(String prop3) {
this.prop3 = prop3;
}
}
CSV 编写器
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
public class CSVWriter<T> {
private List<T> dataList;
CSVWriter(List<T> dataList){
this.dataList = dataList;
}
public void setDataList(List<T> dataList) {
this.dataList = dataList;
}
public List<T> getDataList() {
return dataList;
}
void save(FileWriter fileWriter) {
if(dataList == null) {
throw new RuntimeException("the data was not initialized");
}
//TODO Write all attributes into csv file
}
void save(FileWriter fileWriter, List<String> attributeNames) throws NoSuchFieldException, IllegalAccessException, IOException {
if(dataList == null) {
throw new RuntimeException("the data was not initialized");
}
//TODO improve errors handle
for (T data : dataList) {
Class<?> c = data.getClass();
for (String attribute : attributeNames) {
Field field = c.getDeclaredField(attribute);
String valueString = getValue(field, data);
fileWriter.write(valueString + ",");
}
fileWriter.write("\n");
fileWriter.flush();
}
fileWriter.close();
}
private String getValue(Field field, T data) throws IllegalAccessException {
field.setAccessible(true); //due to private field
return field.get(data).toString();
}
}
如何使用?
public static void main(String[] args) throws Exception {
List<Dummy> dummyList = new ArrayList<>();
Dummy dummy1 = new Dummy();
dummy1.setProp1("prop 1 value");
dummy1.setProp2(2);
dummy1.setProp3("prop 3 value");
dummyList.add(dummy1);
CSVWriter<Dummy> csvWriter = new CSVWriter<>(dummyList);
csvWriter.save(new FileWriter("group1.csv"), Dummy.GROUP_1);
csvWriter.save(new FileWriter("group2.csv"), Dummy.GROUP_2);
System.out.println("Hello World!");
}
我知道您需要使用 OpenCSV 来完成。这个库我没用过,不过概念应该是一样的。
如何仅将特定的 bean 属性写入 csv 文件(使用 openCSV)
即给定 class
public class MyEntry {
private String propOne;
private Long propTwo;
private int propThree;
private String propFour;
private String propFive;
...
private String propFifteen;
//getters and setter omitted for brevity
}
可能存在我需要创建一个包含从 propFour
到 propTen
字段的 csv 文件的情况,在其他情况下,可能需要创建一个包含所有 bean 属性的 csv。
可能有很多情况需要将不同的字段导出到 csv 文件。如何在不为不同情况创建自定义 classes 的情况下实现它?
您可以创建 CSVWriter.java 并使用反射概念读取您的 class 的属性。请记住调用 setAccessible(true) 以便从外部访问私有字段
虚拟对象
import java.util.List;
public class Dummy {
public static final List<String> GROUP_1 = List.of("prop1", "prop2");
public static final List<String> GROUP_2 = List.of("prop1", "prop3");
private String prop1;
private Integer prop2;
private String prop3;
public String getProp1() {
return prop1;
}
public Integer getProp2() {
return prop2;
}
public String getProp3() {
return prop3;
}
public void setProp1(String prop1) {
this.prop1 = prop1;
}
public void setProp2(Integer prop2) {
this.prop2 = prop2;
}
public void setProp3(String prop3) {
this.prop3 = prop3;
}
}
CSV 编写器
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
public class CSVWriter<T> {
private List<T> dataList;
CSVWriter(List<T> dataList){
this.dataList = dataList;
}
public void setDataList(List<T> dataList) {
this.dataList = dataList;
}
public List<T> getDataList() {
return dataList;
}
void save(FileWriter fileWriter) {
if(dataList == null) {
throw new RuntimeException("the data was not initialized");
}
//TODO Write all attributes into csv file
}
void save(FileWriter fileWriter, List<String> attributeNames) throws NoSuchFieldException, IllegalAccessException, IOException {
if(dataList == null) {
throw new RuntimeException("the data was not initialized");
}
//TODO improve errors handle
for (T data : dataList) {
Class<?> c = data.getClass();
for (String attribute : attributeNames) {
Field field = c.getDeclaredField(attribute);
String valueString = getValue(field, data);
fileWriter.write(valueString + ",");
}
fileWriter.write("\n");
fileWriter.flush();
}
fileWriter.close();
}
private String getValue(Field field, T data) throws IllegalAccessException {
field.setAccessible(true); //due to private field
return field.get(data).toString();
}
}
如何使用?
public static void main(String[] args) throws Exception {
List<Dummy> dummyList = new ArrayList<>();
Dummy dummy1 = new Dummy();
dummy1.setProp1("prop 1 value");
dummy1.setProp2(2);
dummy1.setProp3("prop 3 value");
dummyList.add(dummy1);
CSVWriter<Dummy> csvWriter = new CSVWriter<>(dummyList);
csvWriter.save(new FileWriter("group1.csv"), Dummy.GROUP_1);
csvWriter.save(new FileWriter("group2.csv"), Dummy.GROUP_2);
System.out.println("Hello World!");
}
我知道您需要使用 OpenCSV 来完成。这个库我没用过,不过概念应该是一样的。