Example 1: Get current working directory
fun main(args: Array<String>) {
val path = System.getProperty("user.dir")
println("Working Directory = $path")
}
When you run the program, the output will be:
Working Directory = C:\Users\Admin\Desktop\currDir
In the above program, we used System’s getProperty() method to get the user.dir property of the program. This returns the directory which contains our Java project.
Example 2: Get current working directory using Path
import java.nio.file.Paths
fun main(args: Array<String>) {
val path = Paths.get("").toAbsolutePath().toString()
println("Working Directory = $path")
}
When you run the program, the output will be:
Working Directory = C:\Users\Admin\Desktop\currDir