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) }
}
Answer from Anisuzzaman Babla on Stack OverflowBaeldung
baeldung.com โบ home โบ kotlin โบ kotlin io โบ reading from a file in kotlin
Reading from a File in Kotlin | Baeldung on Kotlin
April 28, 2026 - Reads a file line by line using the specified charset (default is UTF-8) and calls action for each line: fun readFileLineByLineUsingForEachLine(fileName: String) = File(fileName).forEachLine { println(it) }
ZetCode
zetcode.com โบ kotlin โบ readfile
Kotlin read file - reading file in Kotlin
January 29, 2024 - With forEachIndexed we add a line number to each line. File.readText gets the entire content of this file as a String.
Videos
08:42
Kotlin Day 13 | File IO in Kotlin Explained | Programming with ...
07:00
Efficient IO With Buffered Reading & Writing In Kotlin - IO ...
18:16
Working With Files In Kotlin - IO Essentials - YouTube
15:09
InputStreams & OutputStreams In Kotlin - IO Essentials - YouTube
05:40
Kotlin How to Read Text File - YouTube
Top answer 1 of 8
86
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) }
}
2 of 8
40
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)
}
TutorialKart
tutorialkart.com โบ kotlin โบ read-contents-of-a-file-in-kotlin
How to Read a File in Kotlin using BufferedReader or InputStream
June 19, 2017 - File.readText() : To read contents of file to a single String ยท File.useLines() : To process a large text file lazily ... Prepare file object with the location of the file passed as argument to File class constructor.
Kotlin
kotlinandroid.org โบ kotlin android home โบ kotlin tutorials โบ kotlin file operations โบ kotlin โ read file to string
Kotlin โ Read File to String
... import java.io.File fun main() ... this Kotlin File Operations tutorial, we have seen how to read a text file to a string using java.io.File.readText() method, with a well detailed step by step process, and an example program....
Kotlin
kotlinlang.org โบ api โบ latest โบ jvm โบ stdlib โบ kotlin.io โบ java.io.-file โบ read-text.html
readText - Kotlin Programming Language
December 30, 2022 - The Kotlin Standard Library provides ... with Kotlin. These include: Higher-order functions implementing idiomatic patterns (let, apply, use, synchronized, etc). Extension functions providing querying operations for collections (eager) and sequences (lazy). Various utilities for working with strings and char sequences. Extensions for JDK classes making it convenient to work with files, IO, and ...
Programiz
programiz.com โบ kotlin-programming โบ examples โบ string-from-file
Kotlin Program to Create String from Contents of a File
import java.nio.charset.Charset import java.nio.file.Files import java.nio.file.Paths fun main(args: Array<String>) { val path = System.getProperty("user.dir") + "\\src\\test.txt" val encoding = Charset.defaultCharset(); val lines = Files.readAllLines(Paths.get(path), encoding) println(lines) } ... In the above program, we use System's user.dir property to get the current directory stored in the variable path. Check Kotlin Program to get the current directory for more information.
Kotlin
kotlinandroid.org โบ kotlin android home โบ kotlin tutorials โบ kotlin file operations โบ kotlin โ read file
Kotlin โ Read File
To reading a file in Kotlin, you can use the readText() method of the File class from the java.io package. This method reads the entire content of a file into a single String, making it convenient for smaller files.
FrontBackend
frontbackend.com โบ kotlin โบ how-to-read-a-file-in-kotlin
How to read a file in Kotlin | FrontBackend
March 17, 2021 - import java.io.File import java.io.InputStream fun main(args: Array<String>) { val inputStream: InputStream = File("frontbackend.txt").inputStream() val out = mutableListOf<String>() inputStream.bufferedReader().useLines { lines -> lines.forEach { out.add(it) } } out.forEach { println(it) } } ... In this short article, we presented ways to read a file in Kotlin.
TutorialKart
tutorialkart.com โบ kotlin โบ reads-file-content-as-a-list-of-lines-in-kotlin-example
How to read file content as a list of lines in Kotlin - Kotlin Example
August 19, 2017 - To read file content as a List<String> in Kotlin, use the File.readLines() extension function from java.io.File.
Techie Delight
techiedelight.com โบ read-a-text-file-into-string-in-kotlin
Read a text file into a string in Kotlin
March 12, 2022 - Techie Delight is a leading platform for technical interview preparation. Prepare for your coding interview with Techie Delight. Improve your algorithmic skills and crack interviews with top tech companies.
GeeksforGeeks
geeksforgeeks.org โบ read-from-files-using-inputreader-in-kotlin
Read From Files using InputReader in Kotlin - GeeksforGeeks
March 13, 2022 - If we need to read a file located in a different folder, it looks similar to the following: ... This piece of code simply takes all the text in the file and prints it on the console. Another way of reading file contents is by directly creating a reader of the file as we do in this code: ... import java.io.File fun main (args: Array<String>) { val inputString = File("gfg.txt").reader().use{it.readText()} println (inputString) }
GitHub
gist.github.com โบ laaptu โบ 6459577
Android read file as string from asset ยท GitHub
import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.widget.Toast import kotlinx.android.synthetic.main.activity_main.* import java.io.BufferedReader import java.io.IOException import java.io.InputStreamReader ... override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val dictfile = getString(R.string.TestFile) try { val fIn = (assets.open(dictfile)) val file = InputStreamReader (fIn) val br = BufferedReader (file) var line = br.readLine () val all = StringBuilder () while (line!= null) { all.append (line + "\n") line = br.readLine () } br.close () file.close () Results.text = all } catch (e: IOException) { Toast.makeText (this, " Could not read the file called < $dictfile > ", Toast.LENGTH_LONG) .show () } }
Kotlin
kotlinlang.org โบ api โบ core โบ kotlin-stdlib โบ kotlin.io.path โบ read-lines.html
readLines | Core API โ Kotlin Programming Language
kotlin-stdlib/kotlin.io.path/readLines ยท JVM ยท inline fun Path.readLines(charset: Charset = Charsets.UTF_8): List<String>(source) Reads the file content as a list of lines. It's not recommended to use this function on huge files. For reading lines of a large file or a file of unknown size, use Path.forEachLine or Path.useLines.
Chercher
chercher.tech โบ null โบ null โบ read and write files in kotlin
Read and write files in Kotlin
August 29, 2018 - Chercher tech provides the tutorials for the future technologies, We are writing an article one per day Actually the author is not a developer, he is a tester first author thought to write a tutorial only for selenium then he thought for protractor python and then UI path now writing kotlin file operations ยท bufferedReader() to read contents of a file into BufferedReader