I am trying to read a csv file using jackson but whenever I actually go to declare the file, i get the below exception:
IllegalStateException: classLoader.getResource("/src/integTest/testdata/Tests.csv") cannot be null
import com.fasterxml.jackson.databind.MappingIterator
import com.fasterxml.jackson.dataformat.csv.CsvMapper
import java.io.File
class CsvReader(private val fileName: String, private val testCase: TestCase) {
fun readCsv() {
// configure schema and reader
val mapper = CsvMapper()
val schema = mapper.schemaFor(testCase.javaClass)
val objectReader = mapper.readerFor(testCase.javaClass).with(schema)
//TODO this returns: java.lang.IllegalStateException: javaClass.getResource(fileName) must not be null
val reader = File(javaClass.getResource(fileName).file).reader()
//read file
val mappingIterator: MappingIterator<TestCase> = objectReader.readValues(reader)
while (mappingIterator.hasNext()) {
//TODO get fields from read and return a TestCase Object
println(mappingIterator.next())
}
}
I am initializing the class using the following file path as a parameter:
var FILE_NAME: String = "/src/integTest/kotlin/testdata/testCase.csv"
Anyone else ever encountered this issue?
Gradle seemed to be looking for the file in a src/resources directory.
In addition kotlin didnt seem to play nicely with java so I added the Jackson Kotlin module
Played around with generics so the code could be reusable elsewhere. Seems to work nicely now.
Here is the new code:
import com.fasterxml.jackson.databind.MappingIterator
import com.fasterxml.jackson.dataformat.csv.CsvMapper
import com.fasterxml.jackson.dataformat.csv.CsvSchema
import com.fasterxml.jackson.module.kotlin.KotlinModule
import org.springframework.core.io.ClassPathResource
inline fun <reified T> readCsv(filename: String): List<T> {
// configure mapper, schema and reader
val mapper = CsvMapper().apply { registerModule(KotlinModule()) }
val bootstrapSchema = CsvSchema.emptySchema().withHeader()
val objectReader = mapper.readerFor(T::class.java).with(bootstrapSchema)
//Read File and produce list from CSV
ClassPathResource(filename).inputStream.use {
val mappingIterator: MappingIterator<T> = objectReader.readValues(it)
val items = mutableListOf<T>()
mappingIterator.forEach { items.add(it) }
return items.toList()
}
}
File(javaClass.getResource(fileName).file).reader()
This is the problematic code. You are trying to read a resource as a file - but a resource is no file. It might be stored in your file system, it might be packaged inside a JAR file etc - this is why Java returns a URL
instead of a File
when you call Class.getResource
.
Instead, you should be using the Class.getResourceAsStream
method and convert that to a reader:
javaClass.getResourceAsStream(fileName)?.let { InputStreamReader(it) }
Note that getResourceAsStream
might return null if the resource was not found.
The below code helps,
fun getResource(fileName: String): String {
val clazzLoader = this::class.java.classLoader
val stream = clazzLoader.getResourceAsStream(fileName)
val reader = stream.bufferedReader()
if (!Objects.isNull(reader)) {
val data = reader.readText()
reader.close()
return data
}
throw RuntimeException("The requested file $fileName not found")
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.