In this tutorial, I will show you many examples that transform a Kotlin Map using map, mapTo, mapKeys, mapKeysTo, mapValues, mapValuesTo.
Related Posts:
– Kotlin HashMap tutorial with examples
– Kotlin List & Mutable List tutorial with examples
In this tutorial, I will show you many examples that transform a Kotlin Map using map, mapTo, mapKeys, mapKeysTo, mapValues, mapValuesTo.
Related Posts:
– Kotlin HashMap tutorial with examples
– Kotlin List & Mutable List tutorial with examples
In this tutorial, I will show you how to read File in Kotlin using InputStream
or BufferedReader
or File
directly.
Related Posts:
– Kotlin Android – Read JSON file from assets using Gson
– Ways to write to File in Kotlin
First, we need a file to read. So create a file named bezkoder.txt with following content:
bezkoder.com
-> Programming Tutorial
-> Becoming zKoder
-> Master Programming
Because we don’t indicate path for bezkoder.txt file, so you should put it in the root folder of the Project (the folder src is located in).
If you wanna read file in resources folder \app\src\main\res\, just change the path:
File("bezkoder.txt")
-> File("src/main/res/bezkoder.txt")
Steps to do:
bufferedReader()
methodCloseable.use()
method along with Reader.readText()
method inside block.Closeable.use(Reader.readText())
Note:
– Reader
implements Closeable
– Closeable.use()
will automatically close the input at the end of the lambda’s execution
Practice:
import java.io.File
import java.io.InputStream
fun main(args: Array<String>) {
val inputStream: InputStream = File("bezkoder.txt").inputStream()
val inputString = inputStream.bufferedReader().use { it.readText() }
println(inputString)
}
Output:
bezkoder.com
-> Programming Tutorial
-> Becoming zKoder
-> Master Programming
Steps to do:
bufferedReader()
methodReader.useLines()
method with Kotlin Sequence (a sequence of all the lines) inside block. It will automatically close the reader once the processing is completeReader.useLines(block: Sequence)
Practice:
import java.io.File
import java.io.InputStream
fun main(args: Array<String>) {
val inputStream: InputStream = File("bezkoder.txt").inputStream()
val lineList = mutableListOf<String>()
inputStream.bufferedReader().useLines { lines -> lines.forEach { lineList.add(it)} }
lineList.forEach{println("> " + it)}
}
Output:
> bezkoder.com
> -> Programming Tutorial
> -> Becoming zKoder
> -> Master Programming
Steps to do:
BufferedReader.use()
method along with Reader.readText()
method inside block.BufferedReader.use(Reader.readText())
Practice:
import java.io.File
import java.io.BufferedReader
fun main(args: Array<String>) {
val bufferedReader: BufferedReader = File("bezkoder.txt").bufferedReader()
val inputString = bufferedReader.use { it.readText() }
println(inputString)
}
Output:
bezkoder.com
-> Programming Tutorial
-> Becoming zKoder
-> Master Programming
Steps to do:
Reader.useLines()
method that will automatically close the reader once the processing is completeReader.useLines(block: Sequence)
Practice:
import java.io.File
import java.io.BufferedReader
fun main(args: Array<String>) {
val bufferedReader = File("bezkoder.txt").bufferedReader()
val lineList = mutableListOf<String>()
bufferedReader.useLines { lines -> lines.forEach { lineList.add(it) } }
lineList.forEach { println("> " + it) }
}
Output:
> bezkoder.com
> -> Programming Tutorial
> -> Becoming zKoder
> -> Master Programming
There are two ways to read all lines of a file using File directly:
File.useLines()
method along with Kotlin SequenceFile.readLines()
method to return a List<String>import java.io.File
import java.io.BufferedReader
fun main(args: Array<String>) {
val lineList = mutableListOf<String>()
File("bezkoder.txt").useLines { lines -> lines.forEach { lineList.add(it) }}
lineList.forEach { println("> " + it) }
// val lineList = File("bezkoder.txt").readLines()
// lineList.forEach { println("> " + it) }
}
Output:
> bezkoder.com
> -> Programming Tutorial
> -> Becoming zKoder
> -> Master Programming
Today we’ve learned many ways to use Kotlin to read a File line-by-line or all lines. Now you can use InputStream or BufferedReader or File directly to do the work.
Happy Learning! See you again.