Write a Kotlin Program to Get Current Working Directory

In Kotlin, getting the current working directory is a simple task.

You can achieve this by using the System.getProperty method with the “user.dir” property.

Here’s a program that demonstrates how to get the current working directory in Kotlin:

fun main() {
val workingDirectory = System.getProperty("user.dir")
println("Current working directory is: $workingDirectory")
}

In this program, we first use the System.getProperty method to get the value of the “user.dir” property, which represents the current working directory.

We store the value in the workingDirectory variable and then print it out to the console using the println method.

When you run this program, you should see the current working directory printed to the console.

It’s worth noting that the current working directory can vary depending on how the program is run.

For example, if you run the program from the command line, the current working directory will be the directory in which you executed the command.

If you run the program from an IDE, the current working directory will be the project directory.

In conclusion, getting the current working directory in Kotlin is a straightforward process that can be accomplished using the System.getProperty method with the “user.dir” property.

With this knowledge, you can now easily retrieve the current working directory in your Kotlin programs.