Java का WatchService
In this page:
What is WatchService?
WatchService, NIO.2 का हिस्सा, किसी program को किसी directory के contents को manually बार-बार poll करने के बजाय creation, deletion, या modification जैसे file system बदलावों के लिए monitor करने देता है।
उदाहरण: What is WatchService?
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
WatchService watcher = FileSystems.getDefault().newWatchService(); // no manual polling needed
System.out.println("WatchService created");
watcher.close();
}
}
Login to try C/C++/Java/PHP code in the editor
Registering a Directory
किसी directory को उसके Path पर register call करके monitor किया जाता है, WatchService और ENTRY_CREATE, ENTRY_DELETE, या ENTRY_MODIFY जैसे specific kinds of events को पास करते हुए जिन पर नज़र रखनी है।
उदाहरण: Registering a Directory
// Import java.nio.file.* so it can be used by its short name
import java.nio.file.*;
// Import java.nio.file.StandardWatchEventKinds.* so it can be used by its short name
import static java.nio.file.StandardWatchEventKinds.*;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) throws Exception {
Path dir = Files.createTempDirectory("watch-demo");
WatchService watcher = FileSystems.getDefault().newWatchService();
dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
// Print a line to the console
System.out.println("Registered " + dir);
watcher.close();
}
}
Login to try C/C++/Java/PHP code in the editor
Watching for File Creation
किसी WatchService पर take() call करना तब तक block करता है जब तक कोई registered event असल में न हो जाए, और returned WatchKey का pollEvents method फिर हर matching event return करता है जो इसके पिछले check होने के बाद हुआ हो।
उदाहरण: Watching for File Creation
import java.nio.file.*;
import static java.nio.file.StandardWatchEventKinds.*;
public class Main {
public static void main(String[] args) throws Exception {
Path dir = Files.createTempDirectory("watch-demo");
WatchService watcher = FileSystems.getDefault().newWatchService();
dir.register(watcher, ENTRY_CREATE);
Files.createFile(dir.resolve("new.txt"));
WatchKey key = watcher.take(); // blocks until the event occurs
for (WatchEvent<?> event : key.pollEvents()) {
System.out.println(event.kind() + " " + event.context());
}
watcher.close();
}
}
Login to try C/C++/Java/PHP code in the editor
Watching for Modification and Deletion
ENTRY_MODIFY तब report करता है जब किसी मौजूदा file का content बदलता है और ENTRY_DELETE तब report करता है जब कोई file हटाई जाती है, किसी program को बिल्कुल किस तरह का file system बदलाव हुआ इसके आधार पर अलग react करने देते हुए।
उदाहरण: Watching for Modification and Deletion
import java.nio.file.*;
import static java.nio.file.StandardWatchEventKinds.*;
public class Main {
public static void main(String[] args) throws Exception {
Path dir = Files.createTempDirectory("watch-demo");
Path file = Files.createFile(dir.resolve("data.txt"));
WatchService watcher = FileSystems.getDefault().newWatchService();
dir.register(watcher, ENTRY_MODIFY, ENTRY_DELETE);
Files.writeString(file, "changed"); // triggers ENTRY_MODIFY
WatchKey key = watcher.take();
for (WatchEvent<?> event : key.pollEvents()) {
System.out.println(event.kind());
}
watcher.close();
}
}
Login to try C/C++/Java/PHP code in the editor
Processing Watch Events in a Loop
WatchService का एक typical उपयोग एक infinite loop है जो events का इंतज़ार करने के लिए take() call करता है, हर एक को process करता है, और फिर key पर reset() call करता है ताकि यह उसी directory पर future events report करना जारी रख सके।
उदाहरण: Processing Watch Events in a Loop
import java.nio.file.*;
import static java.nio.file.StandardWatchEventKinds.*;
public class Main {
public static void main(String[] args) throws Exception {
Path dir = Files.createTempDirectory("watch-demo");
WatchService watcher = FileSystems.getDefault().newWatchService();
dir.register(watcher, ENTRY_CREATE);
Files.createFile(dir.resolve("a.txt"));
WatchKey key = watcher.take();
for (WatchEvent<?> event : key.pollEvents()) {
System.out.println("Event: " + event.kind());
}
key.reset(); // continues reporting future events
watcher.close();
}
}
Login to try C/C++/Java/PHP code in the editor
- events process करने के बाद
key.reset()call न करना, जिससे key बाद के events देना बंद कर देता है। - event के context path को सीधे उपयोग करना, जबकि यह सिर्फ watched folder के relative file name है; इसे directory के खिलाफ resolve करें।
- हर save पर एक event की उम्मीद करना, जबकि editors अक्सर एक file के लिए कई
ENTRY_MODIFYevents पैदा करते हैं।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: