file reader with kotiln

공부는 혼자하는 거·2022년 7월 25일
0

JAVA & Kotlin Tip

목록 보기
8/11

json object Array file reader

    fun <T> readJsonArrayFile(filePath: String): List<Map<*, *>?>? {
        val mapper = ObjectMapper()
        //val readFileAsString = readFileAsString(filePath)
        //println(readFileAsString)
        val myObjects = mapper.readValue(File(filePath), object : TypeReference<List<Map<*, *>?>>() {})
//        for (obj in myObjects) {
//            println(obj)
//        }

        return myObjects
    }
    
    
    
     private fun readFileAsString(filePath: String): String {
        return String(Files.readAllBytes(Paths.get(filePath)))
    }

readDataFromCsv

 fun readDataFromCsv(filePath: String): List<List<String>?> {

        var csvList: MutableList<List<String>?> = mutableListOf()
        val csv = java.io.File(filePath)
        try {
            BufferedReader(FileReader(csv)).use { br ->
                var line: String?
                while (br.readLine().also { line = it } != null) {
                    //println("line  $line")
                    val split: List<String>? = line?.split(",")
                    csvList.add(split)
                }
            }
        } catch (e: IOException) {
            e.printStackTrace()
        }


        return csvList
    }
profile
시간대비효율

0개의 댓글