Java Reference
In-Depth Information
Using the FileWatcher Service to Get Notified about File
Changes
Problem
You want to be notified when some other application updates one or more of the files in
which you are interested.
Solution
Use the Java 7 FileWatchService to get notified of changes to files automatically, instead
of having to examine the files periodically.
Discussion
It is fairly common for a large application to want to be notified of changes to files, without
having to go and look at them periodically. For example, a Java Enterprise web server wants
to know when Servlets and other components get updated. Many modern operating systems
have had this capability for some time, and now it is available in Java.
The basic steps to using the FileWatchService are:
1. Create a Path object representing the directory you want to watch.
2. Get a WatchService by calling, for example, FileSys-
tems.getDefault().newWatchService() .
Search WWH ::




Custom Search