你好,所以我得到了以下代码:
事件处理程序.java
package me.xenopyax.edla.watcher;
import static java.nio.file.StandardWatchEventKinds.*;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchEvent.Kind;
import java.util.ArrayList;
import java.util.List;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
public class EventHandler {
private Path folderPath = Paths.get(System.getProperty("user.dir") + "/Saved Games/Frontier Developments/Elite Dangerous");
private String watchFile;
private List<EventListener> listeners = new ArrayList<>();
public EventHandler() {
// We obtain the file system of the Path
FileSystem fileSystem = folderPath.getFileSystem();
// We create the new WatchService using the try-with-resources block
try (WatchService service = fileSystem.newWatchService()) {
// We watch for modification events
folderPath.register(service, ENTRY_MODIFY);
// Start the infinite polling loop
while (true) {
// Wait for the next event
WatchKey watchKey = service.take();
for (WatchEvent<?> watchEvent : watchKey.pollEvents()) {
// Get the type of the event
Kind<?> kind = watchEvent.kind();
if (kind == ENTRY_MODIFY) {
Path watchEventPath = (Path) watchEvent.context();
// Call this if the right file is involved
if (watchEventPath.toString().equals(watchFile)) {
//File has been modified, call event registered
}
}
}
if (!watchKey.reset()) {
// Exit if no longer valid
break;
}
}
现在我的问题是如何从EventHandler.java中的EventListener.java调用抽象方法以及如何在ArrayList中检查哪些方法被覆盖?我正在尝试创建一个监听文件的 EventHandler,当发生更改时,它会查找更改的内容并从EventListener.java触发适当的抽象方法。
泛舟湖上清波郎朗
相关分类