Created
July 25, 2018 03:41
-
-
Save duqicauc/bcc8d424089abfed2d92b32d7745c9e8 to your computer and use it in GitHub Desktop.
使用WatcherMonitor监控某个目录下文件的创建
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
文件拷贝结束后,才会创建新文件,才会引发文件创建事件