Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

414
Vistas
How to read plain text file in kotlin?

There may be various way to read plain text file in kotlin.

I want know what are the possible ways and how I can use them.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

1. Using BufferedReader

    import java.io.File
    import java.io.BufferedReader

    fun main(args: Array<String>) {
        val bufferedReader: BufferedReader = File("example.txt").bufferedReader()    
        val inputString = bufferedReader.use { it.readText() }
        println(inputString)
    }

2. Using InputStream

Read By Line

    import java.io.File
    import java.io.InputStream

    fun main(args: Array<String>) {
        val inputStream: InputStream = File("example.txt").inputStream()
        val lineList = mutableListOf<String>()

        inputStream.bufferedReader().forEachLine { lineList.add(it) } 
        lineList.forEach{println(">  " + it)}
    }

Read All Lines

    import java.io.File
    import java.io.InputStream

    fun main(args: Array<String>) {
        val inputStream: InputStream = File("example.txt").inputStream()
        val inputString = inputStream.bufferedReader().use { it.readText() }
        println(inputString)
    }

3. Use File directly

    import java.io.File
    import java.io.BufferedReader

    fun main(args: Array<String>) {
        val lineList = mutableListOf<String>()

        File("example.txt").useLines { lines -> lines.forEach { lineList.add(it) }}
        lineList.forEach { println(">  " + it) }
    }
over 4 years ago · Santiago Trujillo Denunciar

0

I think the simplest way to code is using kotlin.text and java.io.File

import java.io.File

fun main(args: Array<String>) {
    val text = File("sample.txt").readText()
    println(text)
}
over 4 years ago · Santiago Trujillo Denunciar

0

The answers above here are all based on Kotlin Java. Here is a Kotlin Native way to read text files:

val bufferLength = 64 * 1024
val buffer = allocArray<ByteVar>(bufferLength)

 for (i in 1..count) {
    val nextLine = fgets(buffer, bufferLength, file)?.toKString()
    if (nextLine == null || nextLine.isEmpty()) break

    val records = parseLine(nextLine, ',')
    val key = records[column]
    val current = keyValue[key] ?: 0
    keyValue[key] = current + 1
}


fun parseLine(line: String, separator: Char) : List<String> {
    val result = mutableListOf<String>()
    val builder = StringBuilder()
    var quotes = 0
    for (ch in line) {
        when {
            ch == '\"' -> {
                quotes++
                builder.append(ch)
            }
            (ch == '\n') || (ch ==  '\r') -> {}
            (ch == separator) && (quotes % 2 == 0) -> {
                result.add(builder.toString())
                builder.setLength(0)
            }
            else -> builder.append(ch)
        }
    }
    return result
}

See: https://github.com/JetBrains/kotlin-native/blob/master/samples/csvparser/src/csvParserMain/kotlin/CsvParser.kt

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda