Kotlin:
Creating a temporary file

How to:

Here’s a quick way to make a temp file in Kotlin:

import java.io.File

fun main() {
    val tempFile = File.createTempFile("myTempFile", ".tmp")

    println("Temporary file created at: ${tempFile.absolutePath}")

    // Write to temp file
    tempFile.writeText("Kotlin is pretty neat, huh?")

    // Delete on exit
    tempFile.deleteOnExit()
}

Output will be something like:

Temporary file created at: /tmp/myTempFile1234567890.tmp

Your temp file path will differ. It’ll have a unique name so don’t sweat over naming clashes.

Deep Dive

The File.createTempFile() method is golden for ad-hoc file generation. It’s been around since Java’s early days and Kotlin, being a JVM language, takes full advantage.

Some alternatives:

  • Files.createTempFile() from java.nio.file offers more control, like setting file attributes.
  • In-memory databases or caches could replace temp files for some use-cases (like H2 or Redis).

By default, temp files are stored in the system’s default temporary file directory, but you can specify your own path. Remember to clean up after yourself; temp files aren’t guaranteed to be deleted after your program runs. The deleteOnExit() method ensures the file is deleted when the JVM shuts down, but it’s not fail-safe for long-running apps.

See Also

More on temp files in Kotlin and Java: