File monitor android6/20/2023 Retrieve the type of event by using the.Process the pending events for the key.If no queued key is available, this method waits. The TimeUnit argument determines whether the specified time is nanoseconds, milliseconds, or some other unit of time. If a queued key is not immediately available, the program waits until the specified time. poll(long, TimeUnit) – Returns a queued key, if one is available.Returns immediately with a null value, if unavailable. poll – Returns a queued key, if available.The order of events in an event processing loop follow: The following code snippet shows how to register a Path instance for all three event types: You do not have to register for the OVERFLOW event to receive it. OVERFLOW – Indicates that events might have been lost or discarded.ENTRY_MODIFY – A directory entry is modified.ENTRY_DELETE – A directory entry is deleted.ENTRY_CREATE – A directory entry is created.StandardWatchEventKinds event types follow: When registering an object with the watch service, you specify the types of events that you want to monitor. (The three-argument version takes a WatchEvent.Modifier, which is not currently implemented.) Register(WatchService, WatchEvent.Kind.). The Path class implements the Watchable interface, so each directory to be monitored is registered as a Path object.Īs with any Watchable, the Path class implements two register methods. Next, register one or more objects with the watch service. WatchService watcher = FileSystems.getDefault().newWatchService() Either run the program in a separate window, or in the background, as follows: WatchDir uses a single thread to process all events, so it blocks keyboard input while waiting for events. Create a test directory that will be passed to the example. WatchDir example to your computer, and compile it. You can dedicate aīecause this API is more advanced, try it out before proceeding. WatchKeys are thread-safe and can be used with the package. Close the service: The watch service exits when either the thread exits or when it is closed (by invoking its closed method).Reset the key, and resume waiting for events.Retrieve each pending event for the key (there might be multiple events) and process as needed.You can obtain the file name from the key. Retrieve the key from the watcher's queue.When an event occurs, the key is signaled and placed into the watcher's queue. Implement an infinite loop to wait for incoming events.You receive a WatchKey instance for each directory that you register. When registering a directory, you specify the type of events for which you want notification. For each directory that you want monitored, register it with the watcher.Create a WatchService "watcher" for the file system.Here are the basic steps required to implement a watch service: You can use it as is, or you can choose to create a high-level API on top of this mechanism so that it is suited to your particular needs. The WatchService API is fairly low level, allowing you to customize it.
0 Comments
Leave a Reply. |