Java 17 Recipes


-10. Monitoring a Directory for Content Changes


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

8-10. Monitoring a Directory for Content Changes
 Problem
You need to keep track when a directory’s content has changed (for example, a file was 
added, changed, or deleted) and act on those changes.
 Solution
By using a java.nio.file.WatchService interface, you can subscribe to be notified about 
events occurring within a folder. In the following example, we subscribe for ENTRY_
CREATE, ENTRY_MODIFY, and ENTRY_DELETE events.
public class Ch_8_10_MonitorFolder {
private void start() {
try {
System.out.println("Watch Event, press q to exit");
FileSystem fileSystem = FileSystems.getDefault();
WatchService service = fileSystem.newWatchService();
Path path = fileSystem.getPath(".");
System.out.println("Watching :"+path.toAbsolutePath());
ChApTer 8 InpuT And OuTpuT


320
path.register(service, StandardWatchEventKinds.ENTRY_CREATE, 
StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.
ENTRY_MODIFY);
boolean shouldContinue = true;
while(shouldContinue) {
WatchKey key = service.poll(250, TimeUnit.MILLISECONDS);
// Code to stop the program
while (System.in.available() > 0) {
int readChar = System.in.read();
if ((readChar == 'q') || (readChar == 'Q')) {
shouldContinue = false;
break;
}
}
if (key == null) continue;
key.pollEvents().stream()
.filter((event) -> !(event.kind() == 
StandardWatchEventKinds.OVERFLOW))
.map((event) -> (WatchEvent
)event).
forEach((ev) -> {
Path filename = ev.context();
System.out.println("Event detected :"+filename.
toString()+" "+ev.kind());
});
boolean valid = key.reset();
if (!valid) {
break;
}
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
public static void main (String[] args) {
Ch_8_10_MonitorFolder monitorFolder = new Ch_8_10_MonitorFolder();
ChApTer 8 InpuT And OuTpuT


321
monitorFolder.start();
}
}
}
The output - if a document is created and then removed - is:
Watch Event, press q to exit
Watching :C:\eclipse-workspace\java17Recipes\.
Event detected :test.txt ENTRY_CREATE
Event detected :test.txt ENTRY_DELETE
 How It Works
NIO.2 includes a built-in polling mechanism to monitor for changes in the file system. 
Using a poll mechanism allows you to wait for events and poll for updates at a specified 
interval. Once an event occurs, you can process and consume it. A consumed event tells 
the NIO.2 framework that you are ready to handle a new event.
To start monitoring a folder, create a WatchService that you can use to poll for 
changes. After the WatchService has been created, register the WatchService with a 
path. A path symbolizes a folder in the file system. When the WatchService is registered 
with the path, you define the kinds of events you want to monitor (see Table 
8-1
). They 
are declared on the java.nio.file package’s StandardWatchEventKinds class.

Download 3.2 Mb.

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




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