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 - We should pass the full path of ... path in our resources for the last two. Reads a file line by line using the specified charset (default is UTF-8) and calls action for each line:...
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)
}
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
Android Kotlin Demo: Using the readLines method and the ...
07:29
How to Read CSV Files Line-by-Line in Kotlin: A Step-by-Step Guide ...
ZetCode
zetcode.com โบ kotlin โบ readfile
Kotlin read file - reading file in Kotlin
January 29, 2024 - ... The Battle of Thermopylae was ... during the second Persian invasion of Greece. In the examples, we use this text file. File.readLines reads the file content as a list of lines......
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 - For most small text files, File.readText() is the shortest option. For line-by-line processing, File.forEachLine(), File.useLines(), or a BufferedReader is usually a better choice.
TutorialKart
tutorialkart.com โบ kotlin โบ kotlin-read-file-line-by-line
How to read a file line by line in Kotlin?
May 9, 2023 - In the following program, we read the text file info.txt line by, and print these lines to standard output. ... import java.io.File import java.io.IOException import java.util.* fun main() { val fileName = "info.txt" try { val file = File(fileName) val sc = Scanner(file) while (sc.hasNextLine()) { val line = sc.nextLine() println(line) } } catch (e: IOException) { e.printStackTrace() } } ... In this Kotlin Tutorial, we learned how to read a file line by line using java.util.Scanner class.
Kotlin
kotlinandroid.org โบ kotlin android home โบ kotlin tutorials โบ kotlin file operations โบ kotlin โ read text file line by line
Kotlin โ Read Text File Line by Line - Kotlin Android
Inside the try block, create a BufferedReader by passing a FileReader that reads from the file specified by filePath. Use a while loop to read the file line by line.
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 - For large files, use useLines() to process the file line by line. You can call readLines() and then use forEachIndexed to access both the index and the line text. </> Copy ยท import java.io.File fun main() { File("file.txt").readLines().for...
CodeSignal
codesignal.com โบ learn โบ courses โบ fundamentals-of-text-data-manipulation-in-kotlin โบ lessons โบ reading-files-line-by-line-in-kotlin-and-summing-integer-values
Reading Files Line-by-Line in Kotlin and Summing Integer ...
To read a file line-by-line in Kotlin, you can use the Files.readAllLines method to read the lines into a list and then iterate over the list using forEach.
FrontBackend
frontbackend.com โบ kotlin โบ how-to-read-a-file-in-kotlin
How to read a file in Kotlin | FrontBackend
March 17, 2021 - For a sake of simplicity let's put it into a folder with our sample Kotlin files. The first approach use BufferedReader and use method: ... import java.io.File import java.io.InputStream fun main(args: Array<String>) { val inputStream: InputStream = File("frontbackend.txt").inputStream() val content = inputStream.bufferedReader().use { it.readText() } println(content) }
Sling Academy
slingacademy.com โบ article โบ reading-files-line-by-line-in-kotlin
Reading Files Line by Line in Kotlin - Sling Academy
November 30, 2024 - The simplest way to read a file line by line in Kotlin is by using BufferedReader.
Mkyong
mkyong.com โบ home โบ kotlin โบ kotlin โ reading large files efficiently
Kotlin - Reading Large Files Efficiently - Mkyong.com
March 3, 2025 - Kotlin provides multiple ways to read large files efficiently. Letโs explore them: The BufferedReader reads text from an input stream while buffering characters for efficient reading. ... import java.io.File fun main() { val file = File("largefile.txt") file.bufferedReader().use { reader -> reader.lineSequence().forEach { line -> println(line) // Process each line } } }
Kotlin
kotlinlang.org โบ api โบ latest โบ jvm โบ stdlib โบ kotlin.io โบ read-line.html
Kotlinlang
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 ...
Java Code Geeks
javacodegeeks.com โบ home โบ jvm languages โบ kotlin
Efficiently Reading Large Files in Kotlin - Java Code Geeks
April 11, 2025 - When working with large files in Kotlin, selecting the right approach is crucial: BufferedReader is best for reading large text files line by line, useLines with Sequence is ideal for the lazy processing of text files, and InputStream with Buffer ...
Kotlin
kotlinandroid.org โบ kotlin android home โบ kotlin tutorials โบ kotlin file operations โบ kotlin โ read csv file line by line
Kotlin โ Read CSV File Line by Line - Kotlin Android
To read a CSV file line by line, read the file into a File object, and then use File.forEachLine() function to process the file line by line. In this tutorial, we will go through the process of reading a CSV file line by line using Kotlin, with an example.