Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitor subfolders with a Java watch service

I am using watchKey to listen for a file change in a particular folder.

Path _directotyToWatch = Paths.get("E:/Raja");
WatchService watcherSvc = FileSystems.getDefault().newWatchService();
WatchKey watchKey = _directotyToWatch.register(watcherSvc, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);

while (true) {
    watchKey=watcherSvc.take();
    for (WatchEvent<?> event: watchKey.pollEvents()) {
        WatchEvent<Path> watchEvent = castEvent(event);
        System.out.println(event.kind().name().toString() + " " + _directotyToWatch.resolve(watchEvent.context()));
        watchKey.reset();
    }
}


It working fine for me. If I modify a file in raja folder it gives me the file name with path. But, when I put some files in subfolders like "E:/Raja/Test", it gives me only the path where I put it, not the file name.

How to get the file name?

like image 878
Raja Avatar asked May 17 '13 14:05

Raja


People also ask

What is watch Service Java?

A watch service that watches registered objects for changes and events. For example a file manager may use a watch service to monitor a directory for changes so that it can update its display of the list of files when files are created or deleted.

What is file watcher in Java?

The java. nio. file package provides a file change notification API, called the Watch Service API. It enables us to register a folder with the watch service.

Can we monitor a directory for adding new files in Java?

Create a WatchService "watcher" for the file system. For each directory that you want monitored, register it with the watcher. When registering a directory, you specify the type of events for which you want notification. You receive a WatchKey instance for each directory that you register.


2 Answers

The reason why you're not getting the file name created/modified inside a subfolder is given by Stephen C in his answer.

Here is a simple example of how to register directories and subdirectories to watch them for the events you are interested in:

/**
 * Register the given directory and all its sub-directories with the WatchService.
 */
private void registerAll(final Path start) throws IOException {
    // register directory and sub-directories
    Files.walkFileTree(start, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                throws IOException {
            dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
            return FileVisitResult.CONTINUE;
        }

    });

}

Check out the official Java Tutorials: Watching a Directory for Changes. There you can find very nice explanations and examples with the source code.

Particularly you'll be interested in this example of how to watch a directory (or directory tree) for changes to files: WatchDir.java.

The method I supplied above was taken from this example (omitting some parts for brevity).
Read the tutorial for the details.

like image 77
informatik01 Avatar answered Oct 23 '22 15:10

informatik01


The reason you are only seeing an event for "E:/Raja/Test" and not "E:/Raja/Test/Foo.txt" (for example) is that you've only registered the "E:/Raja" directory with the service. This means you will see events on the directory and its immediate members. The "E:/Raja/Test" is a member of the directory, and you are getting events to say that is has been changed ... when files are added to it.

The solution is to register all of subdirectories of "E:/Raja" as well ... going as far down the directory hierarchy as you need to go.

like image 31
Stephen C Avatar answered Oct 23 '22 14:10

Stephen C