Reading csv file with Kotlin

Asked

Viewed 120 times

1

I am beginner in Kotlin and have the following class to read a csv file:

@Service
class ClienteImportService {
    fun obtemTodosMsisdn(args: Array<File>) {
        val reader = Files.newBufferedReader(Paths.get(toString()))
        val csvToBean = CsvToBeanBuilder(reader)
                .withType(Cliente::class)
                .build()
        val clientes = csvToBean.parse()

        for (cliente in clientes)
            System.out.println(cliente)

    }
}

I’m making a mistake in the line it contains val csvToBean = Csvtobeanbuilder(Reader)

Which is the following:

Type inference failed: Not enough information to infer Parameter T in constructor Csvtobeanbuilder(Reader: Reader!) Please specify it explicitly.

What I do?

  • Have you tried to pass the Java class instead of Kotlin? (withType(Cliente::class.java))

  • Yes! But the error remains :(

  • @Anthonyaccioly, as a thanks and respect to the help, I managed to solve as follows: val csvToBean = Csvtobeanbuilder<Client>(Reader) . withType(Client::class.java) . build()

  • Hi @Mssantana, feel free to post your answer. It is interesting that Kotlin could not infer the generic parameters, that it is good that pass it explicitly solved :).

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.