How to read File in Kotlin

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

Create the file

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")

Kotlin read File using InputStream

Read all lines

Steps to do:

  • retrieve InputStream from File, then get BufferedReader using bufferedReader() method
  • use Closeable.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

Read line by line

Steps to do:

  • retrieve InputStream from File, then get BufferedReader using bufferedReader() method
  • use Reader.useLines() method with Kotlin Sequence (a sequence of all the lines) inside block. It will automatically close the reader once the processing is complete
    Reader.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

Kotlin read File using BufferedReader

Read all lines

Steps to do:

  • create BufferedReader from File
  • use 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

Read line by line

Steps to do:

  • create BufferedReader from File
  • use Reader.useLines() method that will automatically close the reader once the processing is complete
    Reader.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

Kotlin read File directly

There are two ways to read all lines of a file using File directly:

  • File.useLines() method along with Kotlin Sequence
  • File.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

Conclusion

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.

Further Reading