[Java] 纯文本查看 复制代码
import java.io.File;
import java.util.Date;
import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyListener;
public class MainTest {
public static void main(String[] args) {
try {
// path to watch
// 指定要检测的目录
String dir = new File(args.length == 0 ? "." : args[0]).getCanonicalFile().getAbsolutePath();
// watch mask, specify events you care about,or JNotify.FILE_ANY for all events.
// 配置要检测的类型,配置你关心的(新建|删除|修改|重命名),或者直接标记为 JNotify.FILE_ANY ,它会监听所有类型
int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED;
// watch subtree
// 是否检测子目录
boolean watchSubTree = true;
JNotify.addWatch(dir, mask, watchSubTree, new JNotifyListener() {
public void fileRenamed(int wd, String rootPath, String oldName, String newName) {
System.out.println("renamed" + rootPath + " : " + oldName + " -> " + newName);
}
public void fileModified(int wd, String rootPath, String name) {
System.out.println("modified" + rootPath + " : " + name);
if (name.contains("a.txt")) {
System.out.println("["+new Date().toLocaleString()+"]a.txt file was modified !");
}
}
public void fileDeleted(int wd, String rootPath, String name) {
System.out.println("deleted " + rootPath + " : " + name);
}
public void fileCreated(int wd, String rootPath, String name) {
System.out.println("created" + rootPath + " : " + name);
}
});
System.out.println("Monitoring " + dir);
while (true)
Thread.sleep(10000);
} catch (Exception e) {
e.printStackTrace();
}
}
}