我正在尝试使用 WatchService 扫描目录以查找对多个文件的任何更改。我能够识别给定目录中已更改的确切文本文件。我想知道是否有某种方法可以让我的程序准确显示更改是什么,然后在更改中搜索特定变量?
新条目看起来像这样
2015 年 7 月 2 日 11:44, 88, 55
我想拉这条线,然后扫描 88 或 55 是否达到 100 或更高。
这是我当前的代码。
public void scanChanges() {
Path p = Paths.get("C:\\Users\\achamberlain\\Desktop\\Data");
try {
WatchService watcher = p.getFileSystem().newWatchService();
p.register(watcher, ENTRY_CREATE, ENTRY_MODIFY);
for (;;) {
WatchKey key = null;
key= watcher.take();
for (WatchEvent<?> event : key.pollEvents()) {
String location;
location = (p + "\\" + event.context().toString());
Watcher sendlocation = new Watcher();
sendlocation.readFile(location);
key.reset();
}
}
} catch (IOException | InterruptedException e) {
}
}
public class Watcher {
public static void main(String[] args) throws IOException {
Modify modClass = new Modify();
modClass.scanChanges();
}
//changes "location" to "fileName" for use in this class
public void readFile(String fileName){
System.out.println(fileName);
}
}