Read a file line-by-line in Kotlin
This post will discuss how to read a file line-by-line in Kotlin.
1. Using BufferedReader class
The standard solution to read a file line-by-line is using the BufferedReader#readLine() function. Each call to the readLine() function reads the next line from the file and returns it as a string. Following is a simple example demonstrating its usage:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.io.BufferedReader import java.io.File import java.io.FileReader import java.io.IOException fun main() { val file = File("/home/data/file.txt") try { BufferedReader(FileReader(file)).use { br -> var line: String? while (br.readLine().also { line = it } != null) { println(line) } } } catch (e: IOException) { e.printStackTrace() } } |
Alternatively, you can use the BufferedReader#lines() function to shorten the code and improve its readability. It returns the stream of lines of text read from the BufferedReader. A typical invocation for this function would look like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.io.BufferedReader import java.io.File import java.io.FileReader import java.io.IOException fun main() { val file = File("/home/data/file.txt") try { BufferedReader(FileReader(file)).use { br -> br.lines().forEach { println(it) } } } catch (e: IOException) { e.printStackTrace() } } |
2. Using Files class
Another plausible way is to use the Files.lines() function to get all lines from the file as a stream. Ensure that the stream is closed promptly after completion of the stream’s operations.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.io.IOException import java.nio.file.Files import java.nio.file.Paths fun main() { val fileName = "/home/data/file.txt" try { Files.lines(Paths.get(fileName)).use {stream -> stream.forEach { println(it) }} } catch (e: IOException) { e.printStackTrace() } } |
Alternatively, you can directly use the Files.readAllLines() function to get a list of all lines from a file.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.io.IOException import java.nio.file.Files import java.nio.file.Paths fun main() { val fileName = "/home/data/file.txt" try { val lines = Files.readAllLines(Paths.get(fileName)) println(lines) } catch (e: IOException) { e.printStackTrace() } } |
3. Using Scanner class
The Scanner class can read each line of the file using its nextLine() function. It can be used along with the hasNextLine() function that returns true only if the scanner has more lines left, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.io.File import java.io.IOException import java.util.* fun main() { val fileName = "/home/data/file.txt" try { val sc = Scanner(File(fileName)) while (sc.hasNextLine()) { val line = sc.nextLine() println(line) } } catch (e: IOException) { e.printStackTrace() } } |
That’s all about reading a file line-by-line in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)