Java 7 listen for file/directory change

Today I’ve started to experiment with some new features we’ll get in Java 7. First is filesystem WatchService which allows us to listen to changes in some directory without any pooling. Looks really great. You just register to be notified about change and wait to get your notifications. Best of it you can assign as much listeners as you like and even from different threads with no code changes, it all is thread save. Here’s what I’ve done to get notified about files being modified/removed/added to my very important TEMP directory:

public class PathTest {

    private static void listenForChanges(File file) throws IOException {
        Path path = file.toPath();
        if (file.isDirectory()) {
            WatchService ws = path.getFileSystem().newWatchService();
            path.register(ws, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
            WatchKey watch = null;
            while (true) {
                System.out.println("Watching directory: " + file.getPath());
                try {
                    watch = ws.take();
                } catch (InterruptedException ex) {
                List<WatchEvent<?>> events = watch.pollEvents();
                for (WatchEvent<?> event : events) {
                    Kind<Path> kind = (Kind<Path>) event.kind();
                    Path context = (Path) event.context();
                    if (kind.equals(StandardWatchEventKinds.OVERFLOW)) {
                    } else if (kind.equals(StandardWatchEventKinds.ENTRY_CREATE)) {
                        System.out.println("Created: " + context.getFileName());
                    } else if (kind.equals(StandardWatchEventKinds.ENTRY_DELETE)) {
                        System.out.println("Deleted: " + context.getFileName());
                    } else if (kind.equals(StandardWatchEventKinds.ENTRY_MODIFY)) {
                        System.out.println("Modified: " + context.getFileName());
        } else {
            System.err.println("Not a directory. Will exit.");

    public static void main(String[] args) {
        if (args.length > 0) {
            File file = new File(args[0]);
            try {
                System.out.println("Listening on: " + file);
            } catch (IOException ex) {
        } else {
            System.out.println("Pass directory path as parameter");

There is one tricky thing when using WatchService. You have to remember to reset the WatchKey after you poll events, or it will stop getting updates (only first directory change will execute your code, rest won’t do anything)


7 Responses to Java 7 listen for file/directory change

  1. Annasaheb says:

    which tye of the programming tools are u used for this programming.

  2. Pingback: JavaPins

  3. Rajarshi Goswami says:

    It should be StandardWatchEventKinds instaead of StandardWatchEventKind in line ,21,23,15,27

    • Marek Piechut says:

      Updated, thanks.

      Looks like class name changed between Jdk 7 snapshot I’ve used and release.

  4. Srinivas says:

    a very helpful post thankyou

  5. test says:

    very helpful, thnx

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: