5

这是我的代码片段:

 public static void main(String[] args) {

    Try.of(Main::getLines)
            .onFailure(cause -> log.error("An error has occurred while parsing the file", cause));
}

private static List<Fiche> getLines() {
    return Files.lines(Paths.get("insurance_sample.csv"))
            .skip(1)
            .map(Main::toPojo)
            .filter(fiche -> fiche.getPointLongitude().equals(-81.711777))
            .peek(fiche -> log.info("Fiche added with success {}", fiche))
            .collect(Collectors.toList());
}

我正在使用 try-with-resources 或在“finally”子句中关闭此“Stream”。在这条线上Files.lines(Paths.get("insurance_sample.csv"))

任何人都可以帮助我使用 Vavr 使用 try-with-resources 吗?

4

1 回答 1

5

将调用包装Files#lines(Path)Try#withResources这样:

List<String> lines = Try.withResources(() -> Files.lines(Paths.get("/hello.csv")))
        .of(stream -> stream.collect(Collectors.toList()))
        .getOrElseThrow(() -> new IllegalStateException("Something's wrong!"));
于 2018-08-08T09:39:42.803 回答