package test.vfs;
import java.io.File;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.vfs2.FileChangeEvent;
import org.apache.commons.vfs2.FileListener;
import org.apache.commons.vfs2.FileName;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.VFS;
import org.apache.commons.vfs2.impl.DefaultFileMonitor;
/**
* Hello world!
*
*/
public class App {
public static final Log log = LogFactory.getLog(App.class);
public static void main(String[] args) {
FileSystemManager fsManager = null;
FileObject listendir = null;
try {
fsManager = VFS.getManager();
listendir = fsManager.resolveFile(new File("E://vfs").getAbsolutePath());
} catch (FileSystemException e) {
log.error("监视文件夹出错了", e);
e.printStackTrace();
}
System.out.println(listendir.getName());
// 定义一个监视器及事件处理程序
DefaultFileMonitor fm = new DefaultFileMonitor(new FileListener() {
public void fileCreated(FileChangeEvent event) throws Exception {
monitor(event);
}
public void fileDeleted(FileChangeEvent event) throws Exception {
monitor(event);
}
public void fileChanged(FileChangeEvent event) throws Exception {
monitor(event);
}
private void monitor(FileChangeEvent event) {
FileObject fileObject = event.getFile();
FileName fileName = fileObject.getName();
System.out.println(fileName.toString());
}
});
fm.setRecursive(true); // 设置为级联监控
fm.addFile(listendir); // 增加监控文件
fm.start(); // 启动监视器
for(;;){
try {
Thread.currentThread().sleep(1000);
System.out.println(System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}