1. 火狐Cookies的位置
火狐cookies默认地址为%AppData%/Roaming/Mozilla/Firefox/Profiles/随机字符串.default下面
2.监听实现源码
import java.io.IOException;
import java.nio.file.*;
/**
* 监听火狐Cookies变化
*
* @author: Liuhl
* @date: 2020/11/16 0016 12:57
* @description:
*/
public class AlertMain {
public static void main(String[] args) throws IOException, InterruptedException {
//创建一个文件系统的监听服务
WatchService watchService= FileSystems.getDefault().newWatchService();
// 火狐cookies默认地址为%AppData%/Roaming/Mozilla/Firefox/Profiles/随机字符串.default下面
Path path= Paths.get("C:/Users/Administrator/AppData/Roaming/Mozilla/Firefox/Profiles/wuy0ciir.default");
//为该文件夹注册监听,监听新建、修改、删除事件。只能为文件夹(目录)注册监听,不能为单个文件注册监听
path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,StandardWatchEventKinds.ENTRY_MODIFY,StandardWatchEventKinds.ENTRY_DELETE);
//编写事件处理
while (true){ //一直监听
//拉取一个WatchKey。当触发监听的事件时,就会产生一个WatchKey,此WatchKey封装了事件信息。
WatchKey watchKey=watchService.take();
//使用循环是因为这一个WatchKey中可能有多个文件变化了,比如Ctrl+A全选,然后删除,只触发了一个WatchKey,但有多个文件变化了
for (WatchEvent event:watchKey.pollEvents()){
if ("cookies.sqlite".equals(event.context().toString())) {
System.out.println("COOKIE发生了"+event.kind()+"事件!");
} else {
continue;
}
}
watchKey.reset(); //如果不重置,WatchKey使用一次过后就不能再使用,即只能监听到一次文件变化。
}
}
}