java - OpenCSV 标头缺少必填字段 []
问题描述
打开csv 5.1
Caused by: com.opencsv.exceptions.CsvRequiredFieldEmptyException: Header is missing required fields [ALGVERIFICATION, DISTAL MV, LOCATION, PREDICTED STATE, PROXIMAL MV, RUN, SAMPLE TIME]. The list of headers encountered is [].
at com.opencsv.bean.HeaderNameBaseMappingStrategy.captureHeader(HeaderNameBaseMappingStrategy.java:69)
@ParameterizedTest
@ArgumentsSource(MyArgumentsProvider.class)
void test( AlgorithmVerification verifications )
{
Log.d("test", verifications.location);
assertThat(verifications).isNotNull();
}
public enum State {
NA,
ADVANCE,
RETRACT,
}
public static class StateConverter extends AbstractBeanField {
@Override
protected Object convert(String value) {
return State.valueOf(value);
}
}
public static class AlgorithmVerification {
@CsvBindByName(column = "Sample Time", required = true)
protected float sampleTime;
@CsvBindByName(column = "Distal mV", required = true)
protected int distalMV;
@CsvBindByName(column = "Proximal mV", required = true)
protected int proximalMV;
@CsvCustomBindByName(column = "Predicted State", converter = StateConverter.class, required = true)
protected State predictedState;
@CsvBindByName(column = "run", required = true)
protected String run;
@CsvCustomBindByName(column = "Location", converter = StateConverter.class, required = true)
protected String location;
@CsvCustomBindByName(column = "AlgVerification", converter = StateConverter.class, required = true)
protected State algVerification;
}
static class MyArgumentsProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) throws IOException, URISyntaxException {
return Files.list(Paths.get(ClassLoader.getSystemResource("avd").toURI()))
.map(Path::toFile)
.map( f -> Try.withResources( () -> new FileReader(f) )
.of(CsvToBeanBuilder::new)
.map(b -> b.withType(AlgorithmVerification.class) )
.map(CsvToBeanBuilder::build)
.map(CsvToBean::parse)
.getOrElseThrow((throwable) -> new RuntimeException(f.getName(), throwable))
)
.flatMap(List::stream)
.map(Arguments::of);
}
}
这是文件的开头
Sample Time,Distal mV,Proximal mV,Predicted State,run,Location,AlgVerification
0.016,2509,2502,NA,DV-MyString,-1,-1
我错过了一步吗?标题错了吗?我注意到它正在寻找大写标题......但即使那样它也没有找到
解决方案
看起来我对 vavr 的使用Try.withResources
是错误的,应该是这个
return Files.list(Paths.get(ClassLoader.getSystemResource("avd").toURI()))
.map(Path::toFile)
.map( f -> Try.withResources( () -> new FileReader(f) )
.of((fr ) -> new CsvToBeanBuilder<AlgorithmVerification>(fr)
.withType(AlgorithmVerification.class)
.build()
.parse())
.getOrElseThrow((throwable) -> new RuntimeException(f.getName(), throwable))
)
.flatMap(List::stream)
.map(Arguments::of);
推荐阅读
- android - 在 android studio 上使用 xml 绘制形状(可绘制)
- reactjs - 如何
在字符串中使用将字符串放入新行? - sql - 根据已发生的活动更改日期
- c - C 代码、指针及其内容。考试题
- jenkins-pipeline - 参数化 Jenkins 管道使用不同的工作流程
- reactjs - TS 和反应状态 - 'RangeModifier' 类型的参数不可分配给类型的参数
- python - 如何使用 python 提高这个解析器的速度?
- php - 如何为访问 PHP 网站的访问者分配随机会话密钥?
- apache-flink - flink集群启动报错【ERROR】无法正确获取JVM参数
- java - 如何使用在查询中重复的值为 PreparedStatement 创建正确的 sql 查询(字符串)?