首先实现对应接口
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import java.io.File;
public class FilterListener extends FileAlterationListenerAdaptor {
@Override
public void onDirectoryCreate(File directory) {
System.out.println("sdafsdfsafd");
}
@Override
public void onDirectoryChange(File directory) {
System.out.println("sdafsdfsafd");
}
@Override
public void onDirectoryDelete(File directory) {
System.out.println("sdafsdfsafd");
}
}
其次写对应监听器
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.HiddenFileFilter;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import java.io.File;
import java.util.concurrent.TimeUnit;
public class wertwert {
public FileAlterationMonitor getMonitor(){
IOFileFilter directories = FileFilterUtils.and(FileFilterUtils.directoryFileFilter(), HiddenFileFilter.VISIBLE);
IOFileFilter files = FileFilterUtils.and(FileFilterUtils.fileFileFilter(), FileFilterUtils.suffixFileFilter(".log"));
IOFileFilter fileFilter = FileFilterUtils.or(directories,files);
FileAlterationObserver observer = new FileAlterationObserver(new File("E://325345"),fileFilter);
observer.addListener(new FilterListener());
return new FileAlterationMonitor(TimeUnit.SECONDS.toMillis(1),observer);
}
}
最后实现springboot的CommandLineRunner
@Component
class ctt implements CommandLineRunner {
private wertwert wertwert1 = new wertwert();
@Override
public void run(String... args) throws Exception {
System.out.println("asdf");
FileAlterationMonitor fileAlterationMonitor = wertwert1.getMonitor();
try{
fileAlterationMonitor.start();
}catch (Exception e){
System.out.println("asdfasdf");
}
}
}
标签:FileFilterUtils,public,文件夹,io,org,apache,import,分享,监听
From: https://www.cnblogs.com/studymyself/p/17300079.html