Java 17 Recipes


Table 8-1. Types of watchEvents WatchEvent


Download 3.2 Mb.
Pdf ko'rish
bet163/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   159   160   161   162   163   164   165   166   ...   245
Bog'liq
Java 17 Recipes

Table 8-1. Types of watchEvents
WatchEvent
Description
OVERFLOW
An event that has overflown (ignore)
ENTRY_CREATE
A directory or file was created
ENTRY_DELETE
A directory or file has been deleted
ENTRY_MODIFY
A directory or file has been modified
After registering the WatchService with the path, you can “poll” the WatchService 
for event occurrences. By calling the watchService.poll() method, you wait for a 
file/folder event to occur on that path. Using the watchService.poll(int timeout, 
Timeunit timeUnit) wait until the specified timeout is reached before continuing.
ChApTer 8 InpuT And OuTpuT


322
If the WatchService receives an event, or if the allowed time has passed, then it 
continues execution. If there were no events and the timeout was reached, the WatchKey 
object returned by the watchService.poll(int timeout) is null; otherwise, the 
WatchKey object returned contains the relevant information for the event that has 
occurred.
Because many events can occur at the same time (for example, moving an entire 
folder or pasting a bunch of files into a folder), the WatchKey object key might contain 
more than one event. You can use the key to obtain all the events associated with that 
object key by calling the key.pollEvents() method.
The watchKey.pollEvents() call returns a list of watchEvents that can be iterated. 
Each watchEvent contains information on the actual file or folder to which the event 
refers (for example, an entire subfolder could have been moved or deleted) and the 
event type (add, edit, delete). Only those events that were registered in WatchService are 
processed.
Once an event has been processed, it is important to call the EventKey.reset(). 
The reset returns a Boolean value determining whether the WatchKey is still valid. A 
WatchKey becomes invalid if it is canceled or if its originating WatchService is closed. If 
the eventKey returns false, you should break from the watch loop.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   159   160   161   162   163   164   165   166   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling