Questions tagged [watchservice]

WatchService is a Java API for watching changes on a filesystem. It was introduced in Java 7.

WatchService is a Java API for monitoring file systems for changes. It can be used to detect new files, modified files or file deletions. It was introduced in Java 7 and is part of the (new) NIO system.

223 questions
86
votes
8 answers

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?
fedor.belov
  • 22,343
  • 26
  • 89
  • 134
53
votes
2 answers

Is Java 7 WatchService Slow for Anyone Else?

WatchService looks like a great technology but its been too slow to be useful on the OS X and Linux systems I've tested on. To add insult to injury, it doesn't seem to get notified of all events either. This is the case both with my own code and…
sbook
  • 841
  • 1
  • 8
  • 13
44
votes
15 answers

Java 7 WatchService - Ignoring multiple occurrences of the same event

The javadoc for StandardWatchEventKinds.ENTRY_MODIFY says: Directory entry modified. When a directory is registered for this event then the WatchKey is queued when it is observed that an entry in the directory has been modified. The event…
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
33
votes
5 answers

Java WatchService not generating events while watching mapped drives

I implemented a file watcher but I noticed that java nio file watcher doesn't generate events for files being copied on mapped drives. For instance, I've run the file watcher on Unix to watch a local directory (/sharedfolder) which is mapped on…
Ramcis
  • 381
  • 1
  • 4
  • 5
24
votes
4 answers

Why does WatchService generate so many operations?

import java.io.*; import java.nio.file.*; public class Tmp { public static void main(String [] args) throws IOException { int count = 0; Path path = Paths.get("C:\\tmp\\"); WatchService ws = null; try { …
Pawel P.
  • 3,731
  • 4
  • 20
  • 20
18
votes
3 answers

How to watch a folder and subfolders for changes

I´m trying to watch a specific folder for changes, and then if any addition/edition/removal happens inside of it, I need to get the change type of all files in that folder and its subfolders. I'm using WatchService for this but it only watches a…
Yuri Heupa
  • 1,198
  • 3
  • 10
  • 35
17
votes
4 answers

How can I abort Spring-Boot startup?

I'm writing a Spring-Boot application to monitor a directory and process files that are being added to it. I register the directory with WatchService in a configuration class: @Configuration public class WatchServiceConfig { private static…
Zhubin Salehi
  • 281
  • 1
  • 5
  • 14
13
votes
3 answers

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,…
Raja
  • 239
  • 1
  • 5
  • 18
11
votes
3 answers

WatchService and SwingWorker: how to do it correctly?

WatchService sounded like an exciting idea ... unfortunately it seems to be as low-level as warned in the tutorial/api plus doesn't really fit into the Swing event model (or I'm missing something obvious, a not-zero probability Taking the code from…
kleopatra
  • 51,061
  • 28
  • 99
  • 211
11
votes
1 answer

Java7 WatchService - How to detect rename/move of the actual watched directory

I'm using WatchService for synchronization data files with the application workbench. When I rename/move the watched directory I don't get any event nor the WatchKey won't become invalid. I still get events from the renamed directory but as far as I…
rjezek
  • 111
  • 2
  • 5
9
votes
2 answers

Java I/O: Ensure a file is not locked by another process before any r/w operation

I'm encountering a recurrent issue in an application that tracks content of files within a directory, based on the Java 7 WatchService API. When the underlying file system fires a modification event on a file, I want to compute its SHA-256 right…
sylvain
  • 246
  • 2
  • 5
9
votes
2 answers

How can I watch subdirectory for changes with WatchService? (Java)

I want to watch some directory for changes and her subdirectories. I tried to do this with WatchService but I can't know from which directory the file was changed. How can I retrieve the full path from the WatchEvent?
fonet
  • 91
  • 1
  • 2
9
votes
2 answers

Java Watch Service : Not Working for Remote Files mounted in the local Server

I have Java program monitoring a remote folder mounted in my local server. But it is not detecting any changes / modification whenever something changed in the remote folder. It is working fine if the changes / modification is made in the mounted…
Ianthe
  • 5,559
  • 21
  • 57
  • 74
9
votes
2 answers

WatchService fires ENTRY_MODIFY sometimes twice and sometimes once

I am using this WatchService example from Oracle: import java.nio.file.*; import static java.nio.file.StandardWatchEventKinds.*; import static java.nio.file.LinkOption.*; import java.nio.file.attribute.*; import java.io.*; import…
halil
  • 800
  • 16
  • 33
9
votes
2 answers

Java 7 Watch Service ENTRY_CREATE triggered before file is written

I have a watch service watching a directory. Once files are created, I'm processing the directory and updating a tree view. This works fine on ENTRY_DELETE, but sometimes (not always) when a WatchEvent of ENTRY_CREATE occurs, the file has not yet…
Evan Knowles
  • 7,426
  • 2
  • 37
  • 71
1
2 3
14 15