Checking if a directory exists

Kotlin:
Checking if a directory exists

How to:

Kotlin, running on the JVM, leverages the Java File API for file operations, making directory existence checks straightforward. Here’s a basic example:

import java.io.File

fun main() {
    val path = "/path/to/directory"
    val directory = File(path)

    if (directory.exists() && directory.isDirectory) {
        println("Directory exists: $path")
    } else {
        println("Directory does not exist: $path")
    }
}

Sample output, assuming the directory exists:

Directory exists: /path/to/directory

And if it doesn’t:

Directory does not exist: /path/to/directory

In a Kotlin project, you might also frequently work with Kotlin-specific libraries or frameworks, like Ktor for web applications or kotlinx.coroutines for asynchronous programming. However, for checking if a directory exists, the standard Java File API as shown is typically sufficient and widely used due to Kotlin’s interoperability with Java. No third-party libraries are required for this specific task, making it accessible and straightforward for beginners transitioning from other programming languages to Kotlin.