To change this template use File | Settings | File Templates.
<span style="color: #008000;">*/
<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> Example3 {
<span style="color: #0000ff;">public <span style="color: #0000ff;">static <span style="color: #0000ff;">void main(String[] args) <span style="color: #0000ff;">throws<span style="color: #000000;"> Exception{
File directory = <span style="color: #0000ff;">new File("D:/test"<span style="color: #000000;">);
<span style="color: #008000;">//<span style="color: #008000;"> 轮询间隔 5 秒
<span style="color: #0000ff;">long interval = TimeUnit.SECONDS.toMillis(5<span style="color: #000000;">);
<span style="color: #008000;">//<span style="color: #008000;"> 创建一个文件观察器用于处理文件的格式
FileAlterationObserver observer = <span style="color: #0000ff;">new<span style="color: #000000;"> FileAlterationObserver(directory,FileFilterUtils.and(
FileFilterUtils.fileFileFilter(),FileFilterUtils.suffixFileFilter(".txt"<span style="color: #000000;">)));
<span style="color: #008000;">//<span style="color: #008000;">设置文件变化监听器
observer.addListener(<span style="color: #0000ff;">new<span style="color: #000000;"> MyFileListener());
FileAlterationMonitor monitor = <span style="color: #0000ff;">new<span style="color: #000000;"> FileAlterationMonitor(interval,observer);
monitor.start();
<span style="color: #008000;">//<span style="color: #008000;">Thread.sleep(30000);
<span style="color: #008000;">//<span style="color: #008000;">monitor.stop();
<span style="color: #000000;"> }
}
<span style="color: #0000ff;">final <span style="color: #0000ff;">class MyFileListener <span style="color: #0000ff;">implements<span style="color: #000000;"> FileAlterationListener{
@Override
<span style="color: #0000ff;">public <span style="color: #0000ff;">void<span style="color: #000000;"> onStart(FileAlterationObserver fileAlterationObserver) {
System.out.println("monitor start scan files.."<span style="color: #000000;">);
}
@Override
<span style="color: #0000ff;">public <span style="color: #0000ff;">void<span style="color: #000000;"> onDirectoryCreate(File file) {
System.out.println(file.getName()+" director created."<span style="color: #000000;">);
}
@Override
<span style="color: #0000ff;">public <span style="color: #0000ff;">void<span style="color: #000000;"> onDirectoryChange(File file) {
System.out.println(file.getName()+" director changed."<span style="color: #000000;">);
}
@Override
<span style="color: #0000ff;">public <span style="color: #0000ff;">void<span style="color: #000000;"> onDirectoryDelete(File file) {
System.out.println(file.getName()+" director deleted."<span style="color: #000000;">);
}
@Override
<span style="color: #0000ff;">public <span style="color: #0000ff;">void<span style="color: #000000;"> onFileCreate(File file) {
System.out.println(file.getName()+" created."<span style="color: #000000;">);
}
@Override
<span style="color: #0000ff;">public <span style="color: #0000ff;">void<span style="color: #000000;"> onFileChange(File file) {
System.out.println(file.getName()+" changed."<span style="color: #000000;">);
}
@Override
<span style="color: #0000ff;">public <span style="color: #0000ff;">void<span style="color: #000000;"> onFileDelete(File file) {
System.out.println(file.getName()+" deleted."<span style="color: #000000;">);
}
@Override
<span style="color: #0000ff;">public <span style="color: #0000ff;">void<span style="color: #000000;"> onStop(FileAlterationObserver fileAlterationObserver) {
System.out.println("monitor stop scanning.."<span style="color: #000000;">);
}
}