### Java Example for Directory Watching Source: https://github.com/gmethvin/directory-watcher/blob/main/README.md A Java class demonstrating how to set up and use DirectoryWatcher. It configures a listener for file events (CREATE, MODIFY, DELETE) and provides methods to start watching asynchronously or synchronously, and to stop watching. ```java package com.example; import io.methvin.watcher.DirectoryWatcher; import java.io.IOException; import java.nio.file.Path; import java.util.concurrent.CompletableFuture; public class DirectoryWatchingUtility { private final Path directoryToWatch; private final DirectoryWatcher watcher; public DirectoryWatchingUtility(Path directoryToWatch) throws IOException { this.directoryToWatch = directoryToWatch; this.watcher = DirectoryWatcher.builder() .path(directoryToWatch) // or use paths(directoriesToWatch) .listener(event -> { switch (event.eventType()) { case CREATE: /* file created */; break; case MODIFY: /* file modified */; break; case DELETE: /* file deleted */; break; } }) // .fileHashing(false) // defaults to true // .logger(logger) // defaults to LoggerFactory.getLogger(DirectoryWatcher.class) // .watchService(watchService) // defaults based on OS to either JVM WatchService or the JNA macOS WatchService .build(); } public void stopWatching() throws IOException { watcher.close(); } public CompletableFuture watch() { // you can also use watcher.watch() to block the current thread return watcher.watchAsync(); } } ``` -------------------------------- ### Basic RecursiveFileMonitor Usage (Scala) Source: https://github.com/gmethvin/directory-watcher/blob/main/README.md Instantiate and start a RecursiveFileMonitor to watch a directory for file system events. Override onCreate, onModify, and onDelete methods for custom event handling. ```scala import better.files._ import io.methvin.better.files._ val myDir = File("/directory/to/watch/") val watcher = new RecursiveFileMonitor(myDir) { override def onCreate(file: File, count: Int) = println(s"$file got created") override def onModify(file: File, count: Int) = println(s"$file got modified $count times") override def onDelete(file: File, count: Int) = println(s"$file got deleted") } import scala.concurrent.ExecutionContext.Implicits.global watcher.start() ``` -------------------------------- ### Add better-files Dependency (Scala) Source: https://github.com/gmethvin/directory-watcher/blob/main/README.md Add this dependency to your Scala project to enable better-files integration with directory-watcher. ```scala libraryDependencies += "io.methvin" %% "directory-watcher-better-files" % directoryWatcherVersion ``` -------------------------------- ### Add Directory Watcher Dependency for sbt Source: https://github.com/gmethvin/directory-watcher/blob/main/README.md Add this dependency to your sbt build file to include the directory-watcher library. ```scala libraryDependencies += "io.methvin" % "directory-watcher" % directoryWatcherVersion ``` -------------------------------- ### Add Directory Watcher Dependency for Maven Source: https://github.com/gmethvin/directory-watcher/blob/main/README.md Add this dependency to your Maven pom.xml file to include the directory-watcher library. Replace ${directoryWatcherVersion} with the desired version. ```xml io.methvin directory-watcher ${directoryWatcherVersion} ``` -------------------------------- ### Disable File Hashing in Directory Watcher Source: https://github.com/gmethvin/directory-watcher/blob/main/README.md Configure DirectoryWatcher to disable file hashing by setting `fileHashing(false)` during builder initialization. This can improve performance but may lead to duplicate events on some platforms. ```java DirectoryWatcher watcher = DirectoryWatcher.builder() .path(path) .listener(listener) .fileHashing(false) .build(); ``` -------------------------------- ### Use Last Modified Time as File Hasher Source: https://github.com/gmethvin/directory-watcher/blob/main/README.md Configure DirectoryWatcher to use the file's last modified time as the hash for change detection. This is suitable for platforms with millisecond precision for last modified times, such as JDK 10+ on Macs with APFS. ```java DirectoryWatcher watcher = DirectoryWatcher.builder() .path(path) .listener(listener) // use the last modified time as a "hash" to determine if the file has changed .fileHasher(FileHasher.LAST_MODIFIED_TIME) .build(); ``` -------------------------------- ### Override onEvent for Specific Events (Scala) Source: https://github.com/gmethvin/directory-watcher/blob/main/README.md Customize event handling by overriding the onEvent method in RecursiveFileMonitor. This allows you to react to specific event types like creation, modification, or deletion. ```scala import java.nio.file.{Path, StandardWatchEventKinds => EventType, WatchEvent} val watcher = new RecursiveFileMonitor(myDir) { override def onEvent(eventType: WatchEvent.Kind[Path], file: File, count: Int) = eventType match { case EventType.ENTRY_CREATE => println(s"$file got created") case EventType.ENTRY_MODIFY => println(s"$file got modified $count") case EventType.ENTRY_DELETE => println(s"$file got deleted") } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.