Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect filesystem has changed in java

I would like to know how to efficiently implement filesystem changes in java? Say I got a file in a folder and modify that file. I would like to be notified by java about this change as soon as possible(no frequently polling if possible.).

Because I think I could call java.io.file.lastModified every few seconds but I don't like the sound of that solution at all.

alfred@alfred-laptop:~/testje$ java -version
java version "1.6.0_18"
Java(TM) SE Runtime Environment (build 1.6.0_18-b07)
Java HotSpot(TM) Server VM (build 16.0-b13, mixed mode)
like image 414
Alfred Avatar asked Dec 22 '22 04:12

Alfred


1 Answers

Take a look at JNotify, which performs this type of monitoring.

Java 7 will have some more advanced APIs (WatchService) for this sort of work which will eliminate polling on the OSes that support this.

like image 163
Brian Agnew Avatar answered Jan 09 '23 05:01

Brian Agnew