https://grokonez.com/kotlin/kotlin-read-file-kotlin-language
Kotlin – How to read File with Kotlin language
This tutorial shows you how to read File in Kotlin using InputStream
or BufferedReader
.
I. Technology
- Java 1.8
- Kotlin 1.1.2
II. Overview
1. Goal
Read file: all lines/by line usingInputStream
orBufferedReader
orFile
directly.2. Steps to do
- Create:
-
InputStream
fromFile
, then getBufferedReader
usingbufferedReader()
method -
BufferedReader
fromFile
. - Use:
-
Closeable.use()
method withReader.readText()
method inside block.Closeable.use()
will automatically close the input at the end of the lambda's execution:String = (Reader implements Closeable).use(Reader.readText())
-
Reader.useLines()
method with KotlinSequence
(a sequence of all the lines) inside block.Reader.useLines()
will automatically close the reader once the processing is complete:Reader.useLines(block: Sequence)
-
File.useLines()
method with KotlinSequence
(a sequence of all the lines) inside block. It will close the reader once the processing is complete:File.useLines(block: Sequence)
-
File.readLines()
method to return aList<String>
:File.readLines()
III. Practice
0. kotlin.txt
More at:
https://grokonez.com/kotlin/kotlin-read-file-kotlin-language
Kotlin – How to read File with Kotlin language
Top comments (0)