Skip to content

Instantly share code, notes, and snippets.

@duqicauc
Created July 25, 2018 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duqicauc/bcc8d424089abfed2d92b32d7745c9e8 to your computer and use it in GitHub Desktop.
Save duqicauc/bcc8d424089abfed2d92b32d7745c9e8 to your computer and use it in GitHub Desktop.
使用WatcherMonitor监控某个目录下文件的创建
public static void main(String[] args) {
WatchMonitor watchMonitor = WatchMonitor.create("/Users/duqi/", WatchMonitor.ENTRY_CREATE, WatchMonitor.ENTRY_DELETE);
watchMonitor.setWatcher(new Watcher() {
@Override
public void onCreate(WatchEvent<?> event, Path currentPath) {
System.out.println("create1 current time:" + System.currentTimeMillis());
System.out.println("文件创建:" + currentPath);
System.out.println("create2 current time:" + System.currentTimeMillis());
}
@Override
public void onModify(WatchEvent<?> event, Path currentPath) {
System.out.println("文件修改:" + currentPath);
}
@Override
public void onDelete(WatchEvent<?> event, Path currentPath) {
System.out.println("文件删除:" + currentPath);
}
@Override
public void onOverflow(WatchEvent<?> event, Path currentPath) {
System.out.println("文件移除:" + currentPath);
}
});
watchMonitor.start();
System.out.println("copy1 current time:" + System.currentTimeMillis());
FileUtil.copy("/Users/duqi/Downloads/hprof/b.hprof", "/Users/duqi/b.hprfo", true);
System.out.println("copy2 current time:" + System.currentTimeMillis());
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@duqicauc
Copy link
Author

文件拷贝结束后,才会创建新文件,才会引发文件创建事件

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment