ProgramGuru

Files and resource cleanup on the JVM

Files and resource cleanup on the JVM

A program may receive a student name from standard input or store a report on disk. Reading can fail or encounter the end of input, and a relative file path depends on the working directory. Treat these as explicit conditions rather than assuming every resource exists.

What you’ll learn

  • Read and write a temporary JVM file.
  • Keep practice artifacts separate from user data.

Files and resource cleanup on the JVM in the example

  1. Temporary path

    Create an isolated practice artifact.

    unique .txt file
  2. Write and read

    Use the JVM file API with its UTF-8 defaults.

    Diya: 80
  3. Cleanup

    Attempt deletion after the operation leaves its scope.

    deleteIfExists(path)
Attempt deletion after the operation leaves its scope.

Understand files and resource cleanup on the jvm

Kotlin/JVM can use Java file APIs. Choose a temporary artifact for practice so the example does not overwrite a user report. Use a clear encoding and a cleanup path. The online compiler’s filesystem is its execution environment, not your computer’s folders.

Follow files and resource cleanup on the jvm

Save the complete source as Main.kt and use the Kotlin/JVM environment. No separate input file is needed.

Kotlin/JVM · complete program

import java.nio.file.Files
fun main() {
  val path = Files.createTempFile("school-report-", ".txt")
  try {
    Files.writeString(path, "Diya: 80")
    println(Files.readString(path))
  } finally { Files.deleteIfExists(path) }
}

Expected output

Diya: 80

Follow the execution

The JDK creates a unique file. WriteString and readString use their UTF-8 defaults here, and the output shows the stored text. Finally attempts removal after the scope’s operation. The random filename is not printed, keeping the demonstrated result stable.

Practice: Files and resource cleanup on the JVM

Your task: Why avoid an existing report path for practice?

Kotlin/JVM · complete program

import java.nio.file.Files
fun main() {
  val path = Files.createTempFile("school-report-", ".txt")
  try {
    Files.writeString(path, "Diya: 80")
    println(Files.readString(path))
  } finally { Files.deleteIfExists(path) }
}

Worked answer

Writing could replace user data. A unique temporary artifact isolates the exercise and can be cleaned up afterward.

Files and resource cleanup on the JVM: the important boundary

Use use for supported Closeable resources so closing occurs after the block. Filesystem operations can fail because of permissions or storage conditions, including cleanup itself. These APIs are JVM-specific and require the stated JDK environment.

Check your understanding

Why avoid an existing report path for practice?
Choose one answer. You can try again.

Answer and explanation

Writing could replace user data.

Writing could replace user data. A unique temporary artifact isolates the exercise and can be cleaned up afterward.

Files and resource cleanup on the JVM: what to keep in mind

Writing could replace user data. A unique temporary artifact isolates the exercise and can be cleaned up afterward. Use use for supported Closeable resources so closing occurs after the block.

Further reading: official Kotlin documentation for this topic.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Call Java APIs and inspect platform types →