How to read a file with Kotlin?

Asked

Viewed 1,264 times

4

I have a file .txt with some information I’d like to read to a string through Kotlin.

I am a beginner in language and would like to know how to read a file with Kotlin.

  • 2

    Same as Java :P

  • 1

    @mustache I don’t know what it’s like in Java :p

2 answers

6


Most of the Kotlin library is the same as the existing one for Java, one of the great advantages of the language was already born with all this library available.

It is possible read the entire file and then manipulate him, read lines his, or read as you see fit, or read by streams. All documentation what it can do in the most varied ways.

Finally depends on the need. Examples.

Stream

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

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

Line by line

import java.io.File
import java.io.InputStream
 
fun main(args: Array<String>) {
    val inputStream: InputStream = File("kotlination.txt").inputStream()
    val lineList = mutableListOf<String>()
 
    inputStream.bufferedReader().useLines { lines -> lines.forEach { lineList.add(it)} }
    lineList.forEach{println(">  " + it)}
}

Bufferedreader

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

Line by line

import java.io.File
import java.io.BufferedReader
 
fun main(args: Array<String>) {
    val bufferedReader = File("kotlination.txt").bufferedReader()
    val lineList = mutableListOf<String>()
 
    bufferedReader.useLines { lines -> lines.forEach { lineList.add(it) } }
    lineList.forEach { println(">  " + it) }
}

I put in the Github for future reference.

4

(Almost) same as Java

You can use the Java libraries for this. In fact, not only for this, much of the Kotlin library is Java.

Obviously there are several ways to work with files. A simple example of how to read all lines of a file .txt is:

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

fun main(args: Array<String>) {
    val stream: InputStream = File("wallace.txt").inputStream()

    val str = stream.bufferedReader().use { it.readText() }
    println(str)
}

Or read line by line

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

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

    stream.bufferedReader().useLines { l -> l.forEach { linhas.add(it)} }

    linhas .forEach { println(">  " + it) }
}

Obs: Note that it is the default (implicit) name of the parameter of an anonymous function.

I took the examples of Kotlination.

Browser other questions tagged

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