【问题标题】:OpenCSV Header is missing required fields found []OpenCSV 标头缺少必填字段 []
【发布时间】:2020-05-11 19:08:56
【问题描述】:

opencsv 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

我错过了一步吗?标题错了吗?我注意到它正在寻找大写标题......但即使这样它也没有找到

【问题讨论】:

  • 我会确保您正在加载您认为正在加载的文件。
  • @Deadron 嗯。我想我可以/应该在调试器中单步执行它,但是当RuntimeException 抛出它时,它里面有那个文件名。
  • @Deadron 是的,它肯定在查看那个文件

标签: java opencsv vavr


【解决方案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);

【讨论】:

  • 根据我为文档提交的错误 Try.withResources() 已弃用,将在 1.0 中删除
猜你喜欢
  • 2014-08-02
  • 1970-01-01
  • 1970-01-01
  • 2013-05-30
  • 1970-01-01
  • 2016-06-09
  • 1970-01-01
  • 1970-01-01
  • 2021-10-03
相关资源
最近更新 更多