Simple Kotlin library for listening to changes in a file or directory.
Add JitPack to the list of repositories:
repositories {
maven("https://jitpack.io")
}Then add the dependency:
dependencies {
implementation("com.github.gimme:kwatch:1.0.0")
}val file = Path.of("file.txt")
file.onChange {
println("file changed")
}val dir = Path.of("dir")
dir.watch { event ->
println("${event.path}: ${event.actions}") // Prints e.g. "dir/file.txt: [MODIFY]"
}When you want to stop the listener, you simply call cancel on the returned object.
val listener = dir.watch {}
listener.cancel()