Using WatchService in JAVA. Can I watch subdirectory at the same time?

1.6k views Asked by At

In JAVA I will watch directory by using WatchService.

For example if I watch /users/monitor, WatchService can watch only one directory.

But I wanna watch every subdirectory 'at the same time'

watch /users/monitor
/users/monitor/a
/users/monitor/b
...

How can I code? T_T

---------------------------code-------------------------------

package testpack;

import java.nio.file.*;
import java.util.List;
import java.io.*;

class DirectoryFilter implements FileFilter {
    public boolean accept(File file) {
        return file.isDirectory();
    }
}

public class DirectoryWatchExample {
    public static void testForDirectoryChange(Path myDir) {
        while (true) {
            try {
                WatchService watcher = myDir.getFileSystem().newWatchService();
                myDir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE,
                        StandardWatchEventKinds.ENTRY_DELETE,
                        StandardWatchEventKinds.ENTRY_MODIFY);

                WatchKey watckKey = watcher.take();

                List<WatchEvent<?>> events = watckKey.pollEvents();
                for (WatchEvent event : events) {
                    if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
                        System.out.println("Created: "
                                + event.context().toString());
                    }
                    if (event.kind() == StandardWatchEventKinds.ENTRY_DELETE) {
                        System.out.println("Delete: "
                                + event.context().toString());
                    }
                    if (event.kind() == StandardWatchEventKinds.ENTRY_MODIFY) {
                        System.out.println("Modify: "
                                + event.context().toString());
                    }
                }

            } catch (Exception e) {
                System.out.println("Error: " + e.toString());
            }
        }
    }

    public static void main(String[] args) {
        Path myDir = Paths.get("/users/heejoongkim/monitor");
        // define a folder root
        System.out.println("Monitor Start");
        File dir = new File("/users/heejoongkim/monitor");
        testForDirectoryChange(myDir);
    }
}

---------------------------code-------------------------------

1

There are 1 answers

0
MG Developer On BEST ANSWER

You can register multiple directories to the same directory watcher. Depending upon how the sub directory is created. If the directory already exists you can loop over using File.listFiles and the register each directory to the same watcher.

If the file is created after the watcher is registered the you can do the following.

for (WatchEvent<?> event: key.pollEvents()) 
            {
                WatchEvent.Kind<?> kind = event.kind();
                WatchEvent<Path> ev = (WatchEvent<Path>)event;                    
                Path filename = ev.context();
                
                Path child = dir.resolve(filename);
                if(child.toFile().isDirectory() && ev.kind() == ENTRY_CREATE )
                {
                    child.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
                }
                System.out.println(child);
            }