gmethvin / directory-watcher

A cross-platform Java recursive directory watcher, with a JNA macOS watcher and Scala better-files integration
Apache License 2.0
264 stars 34 forks source link

Suggestion: Fail (or handle!) watched path being a file instead of a directory #95

Open brainbytes42 opened 1 year ago

brainbytes42 commented 1 year ago

I just tried out your library, as i found the 'pure' java way using the WatchService kind of ugly - nice work to wrap that with this library!

When playing around, I forgot to get my dummy-file's parent directory when I added the path to your DirectoryWatcher's builder. As a result, I got no events when modifying the file.

The WatchService fails with java.nio.file.NotDirectoryException in this case - with your library everything seemed ok and I didn't get any sing of my mistake other than nothing happen...

So my suggestion would be to either fail for non-directory Paths as java does - or, even better, internally just watch the files parent directory and add a filter to get only events for the respective file. That would be really cool... ;-)

DirectoryWatcher.builder()
                .path(dummyPath.getParent())   // <- initially, I forgot getPatent() for my dummy-textfile
                .listener(event -> System.out.println("  \\-> EVENT = " + event))
                .build()
                .watchAsync();