Kotlin – Get File Modification Date
To get the modification date of a file in Kotlin, you can use the File
class from the java.io
package and the Files
class from the java.nio.file
package. This allows you to determine when the file was last modified.
In this tutorial, we will go through the process of obtaining the last modification date of a file using Kotlin.
Steps to Get the File Modification Date in Kotlin
To retrieve the modification date of a file in Kotlin, follow these steps:
- Import the
File
class from thejava.io
package and theFiles
class from thejava.nio.file
package. - Create a
File
object with the file path. - Convert the
File
object to aPath
object using thetoPath()
method. - Use the
Files.getLastModifiedTime()
method to get the last modification time of the file.
Kotlin Example Program to Get File Modification Date
In the following example program, we demonstrate how to get the last modification date of a file named 'example.txt'
.
Main.kt
import java.io.File
import java.nio.file.Files
fun main() {
val filePath = "example.txt" // Replace with your file path
val file = File(filePath)
val lastModifiedTime = Files.getLastModifiedTime(file.toPath())
println("Last modification date and time: $lastModifiedTime")
}
Output
Last modification date and time: 2023-10-24T13:49:04.492095047Z
The output will display the last modification date and time of the specified file.
Summary
In this Kotlin tutorial, we learned how to obtain the last modification date of a file.