`
wankunde
  • 浏览: 165931 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

apache commons vfs 文件监控

 
阅读更多
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();
			}
		}
	}
}

 

分享到:
评论

相关推荐

    commons-vfs-1.0-src

    5. **事件监听**:VFS提供了文件系统事件监听功能,开发者可以注册监听器来监控文件系统的变化,如文件的创建、修改、删除等,从而实现文件系统的实时监控。 6. **错误处理**:VFS有一套完善的错误处理机制,当操作...

    apache-commons-vfs-examples-2.0-11.el7.x64-86.rpm.tar.gz

    Apache Commons VFS(Virtual File System)是一个用于访问不同类型的虚拟文件系统的Java库,它提供了一个简单的API来访问如本地文件系统、FTP、SFTP、HTTP、HTTPS、WebDAV等不同类型的文件系统。本次提供的压缩包名...

    apache-commons-vfs-ant-2.0-11.el7.x64-86.rpm.tar.gz

    在探讨如何使用apache-commons-vfs-ant-2.0-11.el7.x64-86.rpm.tar.gz压缩包文件之前,我们需要明确几个关键点。首先,该压缩包包含了多个RPM(Red Hat Package Manager)包,这是一个在Red Hat及其衍生系统(如...

    commons-io-2.6.zip

    9. **FileWatcher**: 虽然不直接在`commons-io`库中,但在Apache Commons中的`vfs`模块中,有一个`FileWatcher`接口,它可以监听文件系统的变化,这对于实时监控文件变化的应用程序非常有用。 10. **IOException**:...

    java开源的文件管理系统

    1. **Apache Commons IO**:提供了一系列用于处理文件、目录和流操作的工具类。 2. **Spring Framework**:强大的企业级开发框架,可用于构建文件管理系统的服务层和数据访问层。 3. **Hibernate**:一个流行的ORM...

Global site tag (gtag.js) - Google Analytics