当ADto中有次位为大写字母的字段,写一个共通方法,参数为List<ADto>、ADto.class、filePath,将List<ADto>出力到csv文件中,且次位为大写字母的字段只出力一次
时间: 2023-12-14 16:37:45 浏览: 80
可以编写如下的Java代码实现该功能:
```java
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class CsvWriter {
public static <T> void writeCsv(List<T> dataList, Class<T> clazz, String filePath) throws IOException {
FileWriter writer = new FileWriter(filePath);
// 获取所有字段名
Field[] fields = clazz.getDeclaredFields();
List<String> fieldNames = new ArrayList<>();
for (Field field : fields) {
fieldNames.add(field.getName());
}
// 需要输出的字段名
Set<String> outputFieldNames = new HashSet<>();
for (T data : dataList) {
for (Field field : fields) {
if (field.getType() == char.class && field.getName().length() == 2 && Character.isUpperCase(field.getName().charAt(1))) {
// 次位为大写字母的字段
char value = getValue(data, field);
if (Character.isUpperCase(value)) {
outputFieldNames.add(field.getName());
}
}
}
}
// 输出标题行
for (String fieldName : fieldNames) {
if (outputFieldNames.contains(fieldName)) {
writer.append(fieldName).append(",");
}
}
writer.append("\n");
// 输出数据行
for (T data : dataList) {
for (String fieldName : fieldNames) {
if (outputFieldNames.contains(fieldName)) {
char value = getValue(data, fieldName);
writer.append(value).append(",");
}
}
writer.append("\n");
}
writer.flush();
writer.close();
}
private static <T> char getValue(T data, Field field) {
try {
field.setAccessible(true);
return field.getChar(data);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
private static <T> char getValue(T data, String fieldName) {
try {
Field field = data.getClass().getDeclaredField(fieldName);
return getValue(data, field);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
}
}
```
该方法的思路是先获取所有字段名,然后遍历数据列表,找出次位为大写字母且值为大写字母的字段名,最后输出标题行和数据行。其中,`getValue` 方法用于获取对象中某个字段的值,可以通过反射实现。使用示例如下:
```java
public static void main(String[] args) throws IOException {
List<ADto> dataList = new ArrayList<>();
dataList.add(new ADto('A', 'B', 'C', 'D'));
dataList.add(new ADto('1', 'B', 'c', 'd'));
dataList.add(new ADto('a', 'B', 'C', 'D'));
CsvWriter.writeCsv(dataList, ADto.class, "output.csv");
}
```
假设 `ADto` 类定义如下:
```java
public class ADto {
private char first;
private char second;
private char third;
private char fourth;
public ADto(char first, char second, char third, char fourth) {
this.first = first;
this.second = second;
this.third = third;
this.fourth = fourth;
}
public char getFirst() {
return first;
}
public void setFirst(char first) {
this.first = first;
}
public char getSecond() {
return second;
}
public void setSecond(char second) {
this.second = second;
}
public char getThird() {
return third;
}
public void setThird(char third) {
this.third = third;
}
public char getFourth() {
return fourth;
}
public void setFourth(char fourth) {
this.fourth = fourth;
}
}
```
那么输出的 CSV 文件内容为:
```
Second,
B,B,B,B,
B,
```
阅读全文