In this tutorial, you shall learn how to get the name of a file identified by a given path in Kotlin using property, with example programs.

Kotlin – Get file name

To get the name of a file in Kotlin, you can use the name property of class.

Step to get file name

  1. Consider that we have a file identified by a given path.
  2. Create a file object with the given path using class, and read the name of the file object using property.
val aFile = File("path/to/file")

The property returns a string value representing the name of the file.



For this example, we will consider a file named info.txt at the path mydata/set1/info.txt.

In the following program, we programmatically get the file name using property.



fun main() {
    val filePath = "mydata/set1/info.txt"
    val file = File(filePath)
    val fileName =
    println("File name : $fileName")


File name : info.txt


In this Kotlin Tutorial, we learned how to get the name of a file from a file object using property.