Different ways to read the content of a file in Kotlin

Read contents of a file in Kotlin :

We have a couple of different ways to read the file content in Kotlin. For these examples, we have created one readme.md file with the below content :

codevscolor.com
programming tutorial
and
examples

Each example will read the content from this file and print it out.

You can place it on anywhere you want. Just update the path variable in the below examples.

readLines() :

readLines is not for large files. You can use it to read the contents of small files. It returns the file content as a list of lines.

By default, it uses UTF-8, but you can specify a different charset as its argument.

import java.io.File
fun main() {
    val filePath = "/Users/cvc/IdeaProjects/KotlinSampleProgram/src/readme.md"
    val fileContent: List<string> = File(filePath).readLines()
    fileContent.forEach { println(it) }
}

This example reads the content of readme.md file and put that in the list fileContent. Each line is a new list element. The forEach loop will print each line of the file on a new line.

useLines() :

It reads the file content and calls one callback function with a sequence of all content. Once the processing is done, it closes the reader.

By default, it uses UTF-8, but you can specify a different charset as its argument.

import java.io.File
fun main() {
    val filePath = "/Users/cvc/IdeaProjects/KotlinSampleProgram/src/readme.md"
    val fileContent = mutableListOf<string>()
    File(filePath).useLines { lines -> fileContent.addAll(lines) }
    print(fileContent)
}

Here, we are reading the content of the file to fileContent, a mutable list of string. This program will print the below output :

[codevscolor.com, programming tutorial, and, examples]

readText() :

readText() reads the entire content of the file as a string. By default, it uses UTF-8, but you can specify a different charset as its argument.

This method is not recommended for large files.

import java.io.File

fun main() {
    val filePath = "/Users/cvc/IdeaProjects/KotlinSampleProgram/src/readme.md"

    val fileContent = File(filePath).readText()
    print(fileContent)
}

This will print :

codevscolor.com
programming tutorial
and
examples

InputStreamReader() :

reader methods returns one InputStreamReader. This can be used to read the contents of a file with a while loop :

import java.io.File
import java.io.InputStreamReader

fun main() {
    val filePath = "/Users/cvc/IdeaProjects/KotlinSampleProgram/src/readme.md"

    val fileReader: InputStreamReader = File(filePath).reader()

    var i: Int = fileReader.read()

    while (i != -1) {
        print(i.toChar())
        i = fileReader.read()
    }
}

The read() method of InputStreamReader reads the content from a stream as integer. toChar() converts it to character.

It will print :

codevscolor.com
programming tutorial
and
examples

InputStream :

inputStream() method returns one input stream and we can read the contents as bytes and convert it to string :

import java.io.File
import java.io.InputStream
import java.nio.charset.Charset

fun main() {
    val filePath = "/Users/cvc/IdeaProjects/KotlinSampleProgram/src/readme.md"

    val fileReader: InputStream = File(filePath).inputStream()

    val fileContent = fileReader.readBytes().toString(Charset.defaultCharset())
    println(fileContent)
}

readBytes() reads the content as a byte array. toString converts it to a string.

Output :

codevscolor.com
programming tutorial
and
examples

forEachLine() :

forEachLine reads the content of a file. It reads the content using specified charset. The default charset is UTF-8. We can access each line using it.

import java.io.File

fun main() {
    val filePath = "/Users/cvc/IdeaProjects/KotlinSampleProgram/src/readme.md"

    File(filePath).forEachLine { println(it) }
}

BufferedReader :

BufferedReader is another way to read the content of a file :

import java.io.File

fun main() {
    val filePath = "/Users/cvc/IdeaProjects/KotlinSampleProgram/src/readme.md"

    val content = File(filePath).bufferedReader().readLines()
    print(content)
}

bufferedReader() returns one buffered reader and readLines() returns one list of strings.

Similar tutorials :

Where is the color and why codevscolor ?

Long story short, I love paintings and I paint on weekends. We(me and my wife) have one Youtube channel. Below is a video that I did recently. If you love this please do subscribe to support us 😊❤️❤️