随笔 - 1313, 文章 - 2, 评论 - 154, 阅读 - 424万
  首页  :: 新随笔  :: 管理

apache commons vfs 文件夹监控

Posted on   季枫  阅读(1513)  评论(0编辑  收藏  举报
复制代码
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();
            }
        }
    }
}
复制代码

 

编辑推荐:
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
阅读排行:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!
智读 | 成都会领科技有限公司官网 | 智读App下载 | 每天听本书的博客 | |
点击右上角即可分享
微信分享提示