文件读取
文件内容范本在代码片段的最后注释段,分析可得出Customer 类。我们通过 commons.csv 工具包解析文件。从而获取到 List<CSVRecord> records 类型的数据。但CSVRecord的values 是 String[] 类型的 不符合我们的预期结果。
类型转换
-
BeanWrapperImpl 为 bean 的属性赋值,当需要时做类型转换,走 Property
-
DirectFieldAccessor 为 bean 的属性赋值,当需要时做类型转换,走 Field
这里使用 BeanWrapperImpl,创建BeanWrapperImpl 并做转换前需要有两步操作,
1.设置要转换的对象(通过构造方法 BeanWrapperImpl(Object object) ,或者setWrappedInstance(***) )
2.添加 转换关系(requiredType -propertyEditor)查看源码
PropertyEditorRegistrySupport 字段以 Editors结尾的Map。如果没有设置转换关系则转换(convert***() )时候只会使用 defaultEditors 。此案例中对于Date 的字段类型无法满足,需要添加自定义的类型转换器(PropertyEditorSupport)-自定义时间转换器CustomDateEditor
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.junit.Test;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import java.beans.IntrospectionException;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
/**
* csv 文件读取与类型转换
*/
public class TestCSV {
@Test
public void main() throws IOException, IntrospectionException {
// 目标文件
FileReader fileReader = new FileReader("E:\\Users\\Desktop\\customer.txt");
//示例标头与要读取文件的各个属性设置
String str = "id|firstName|lastName|birthday";
String[] heads = str.split("\\|"); // {"id", "firstName", "lastName", "birth"}
CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
.setDelimiter('|')
.setHeader(heads)
.setSkipHeaderRecord(true)
.build();
// 解析文件并获取记录
CSVParser parser = csvFormat.parse(fileReader);
List<CSVRecord> records = parser.getRecords();
// 设置类型转换器
BeanWrapperImpl beanWrapper = new BeanWrapperImpl();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
beanWrapper.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
// 将String[] 转换为 Customer 类型的数据
List<Customer> customerList = records.stream().map(record -> {
Customer newRecord = new Customer();
beanWrapper.setWrappedInstance(newRecord);
for (int i = 0; i < heads.length; i++) {
beanWrapper.setPropertyValue(heads[i], record.get(i));
}
return newRecord;
}).collect(Collectors.toList());
System.out.println(customerList);
}
}
//id|firstName|lastName|birthday
//1|Stone|Barrett|1964-10-19 14:11:03
//2|Raymond|Pace|1977-12-11 21:44:30
//3|Armando|Logan|1986-12-25 11:54:28
//4|Latifah|Barnett|1959-07-24 06:00:16
//5|Cassandra|Moses|1956-09-14 06:49:28
//6|Audra|Hopkins|1984-08-30 04:18:10
//7|Upton|Morrow|1973-82-04 05:26:05
//8|Melodie|Velasquez|1953-04-26 11:16:26
//9|Sybill|Nolan|1951-06-24 14:56:51
//10|Glenna|Little|1953-08-27 13:15:08
//11|Ingrid|Jackson|1957-09-05 21:36:47
//12|Duncan|Castaneda|1979-01-21 18:31:27
//13|Xaviera|Gillespie|1965-07-18 15:05:22
//14|Rhoda|Lancaster|1990-09-11 15:52:54
//15|Fatima|Combs|1979-06-01 06:58:54
@Data
public class Customer {
private Integer id;
private String firstName;
private String lastName;
private Date birth;
pubilc Customer(){
}
}