Skip to main content
Browse topics

use Function for Resources in Kotlin

2 min read Updated June 30, 2026
Share

Introduction

use Function for Resources is a fundamental concept every Kotlin developer should understand. File I/O reads and writes persistent data on disk. Kotlin wraps Java file APIs with extension functions for cleaner syntax.

The use extension closes Closeable resources automatically, even when exceptions occur. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.

Definition

  • The use extension closes Closeable resources automatically, even when exceptions occur.
  • It is Kotlin’s equivalent of Java’s try-with-resources.
  • Always prefer use when working with files, streams, and database connections.

Syntax

kotlin
File("data.txt").bufferedReader().use { reader ->
    reader.readLine()
}

use Function for Resources in Kotlin Example Program in Kotlin

kotlin
import java.io.File

fun main() {
    val file = File("sample.txt")
    file.writeText("Line 1
Line 2")
    file.bufferedReader().use { reader ->
        reader.forEachLine { line -> println(line) }
    }
    file.delete()
}

Sample Output

plaintext
Line 1
Line 2

When to use

Use file I/O for reading config files, writing logs, or processing CSV/JSON stored on disk.

How it works

  1. The program starts with a main function — the entry point that runs when you execute the file.

  2. The use extension closes Closeable resources automatically, even when exceptions occur.

  3. Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (kotlinc / kotlin). Compare your console output with the sample output shown below.

Best Practices

Common Mistakes

Key Points

  • The use extension closes Closeable resources automatically, even when exceptions occur.
  • It is Kotlin’s equivalent of Java’s try-with-resources.
  • Always prefer use when working with files, streams, and database connections.
  • Test the example locally and verify the output matches the sample.
  • Experiment by changing input values to see how behaviour changes.

Notes

  • This example creates a temporary file in the working directory and deletes it at the end.
  • File examples assume a writable working directory; adjust paths for your environment.

Frequently Asked Questions

What is use Function for Resources in Kotlin?
The use extension closes Closeable resources automatically, even when exceptions occur.
When should I use use Function for Resources?
Use file I/O for reading config files, writing logs, or processing CSV/JSON stored on disk.
How is use Function for Resources different from Java?
Always prefer use when working with files, streams, and database connections.
How do I practice this topic?
Copy the example program into IntelliJ IDEA or Android Studio, run it, then modify values or add print statements to confirm your understanding.

Related tutorials

Search tutorials