Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

420
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda