Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I watch for single file change with WatchService (not the whole directory)?

When I'm trying to register a file instead of a directory java.nio.file.NotDirectoryException is thrown. Can I listen for a single file change, not the whole directory?

like image 460
fedor.belov Avatar asked Apr 27 '13 10:04

fedor.belov


People also ask

How does file watcher work?

You can define a File Watcher job to start a process that monitors for the existence and size of a specific operating system file. When that file reaches the specified minimum size and is no longer growing in size, the File Watcher job completes successfully, indicating that the file has arrived.

What is a file watcher?

File Watcher is an IntelliJ IDEA tool that allows you to automatically run a command-line tool like compilers, formatters, or linters when you change or save a file in the IDE.

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

Just filter the events for the file you want in the directory:

final Path path = FileSystems.getDefault().getPath(System.getProperty("user.home"), "Desktop"); System.out.println(path); try (final WatchService watchService = FileSystems.getDefault().newWatchService()) {     final WatchKey watchKey = path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);     while (true) {         final WatchKey wk = watchService.take();         for (WatchEvent<?> event : wk.pollEvents()) {             //we only register "ENTRY_MODIFY" so the context is always a Path.             final Path changed = (Path) event.context();             System.out.println(changed);             if (changed.endsWith("myFile.txt")) {                 System.out.println("My file has changed");             }         }         // reset the key         boolean valid = wk.reset();         if (!valid) {             System.out.println("Key has been unregisterede");         }     } } 

Here we check whether the changed file is "myFile.txt", if it is then do whatever.

like image 117
Boris the Spider Avatar answered Sep 19 '22 15:09

Boris the Spider


No it isn't possible to register a file, the watch service doesn't work this way. But registering a directory actually watches changes on the directory children (the files and sub-directories), not the changes on the directory itself.

If you want to watch a file, then you register the containing directory with the watch service. Path.register() documentation says:

WatchKey java.nio.file.Path.register(WatchService watcher, Kind[] events, Modifier... modifiers) throws IOException

Registers the file located by this path with a watch service.

In this release, this path locates a directory that exists. The directory is registered with the watch service so that entries in the directory can be watched

Then you need to process events on entries, and detect those related to the file you are interested in, by checking the context value of the event. The context value represents the name of the entry (actually the path of the entry relatively to the path of its parent, which is exactly the child name). You have an example here.

like image 27
mins Avatar answered Sep 19 '22 15:09

mins