Generic Top

I just announced the new Spring Boot 2 material, coming in REST With Spring:


1. Overview

In this quick tutorial, we’ll learn about the various ways of reading a file in Kotlin.

We’ll cover both use cases of reading the entire file as a String, as well as reading it into a list of individual lines.

2. Reading a File

Let’s first create an input file that will be read by Kotlin. We create a file called and place it in a directory that can be accessed by our code.

The file’s contents could be:

Hello to Kotlin. It's:
1. Concise
2. Safe
3. Interoperable
4. Tool-friendly

Now let’s look at the different ways in which we can read this file. We should pass the full path of the file created above as the input to each of the following methods.

2.1. forEachLine

Reads a file line by line using the specified charset (default is UTF-8) and calls an action for each line:

fun readFileLineByLineUsingForEachLine(fileName: String) 
  = File(fileName).forEachLine { println(it) }

2.2. useLines

Calls a given block callback, giving it a sequence of all the lines in a file.

Once the processing is complete, the file gets closed:

fun readFileAsLinesUsingUseLines(fileName: String): List<String>
  = File(fileName).useLines { it.toList() }

2.3. bufferedReader

Returns a new BufferedReader for reading the content of the file.

Once we have a BufferedReader, we can read all the lines in it:

fun readFileAsLinesUsingBufferedReader(fileName: String): List<String>
  = File(fileName).bufferedReader().readLines()

2.4. readLines

Directly reads the contents of the file as a list of lines:

fun readFileAsLinesUsingReadLines(fileName: String): List<String> 
  = File(fileName).readLines()

This method isn’t recommended being used for huge files.

2.5. inputStream

Constructs a new FileInputStream for the file and returns it as a result.

Once we have the input stream, we can convert that into bytes, and then into a complete String.

fun readFileAsTextUsingInputStream(fileName: String)
  = File(fileName).inputStream().readBytes().toString(Charsets.UTF_8)

2.6. readText

Reads the entire content of the file as a String the specified charset (default is UTF-8).

fun readFileDirectlyAsText(fileName: String): String 
  = File(fileName).readText(Charsets.UTF_8)

This method isn’t recommended for huge files and has an internal limitation of 2 GB file size.

3. Conclusion

In this article, we saw the various ways of reading a file in Kotlin. Depending on the use case, we can either opt for reading the file line-by-line or reading it entirely as a text.

The source code for this article can be found in the following GitHub repo.

Generic bottom

I just announced the new Spring Boot 2 material, coming in REST With Spring: