WatchService for Java 6 [英] WatchService for Java 6

查看:130
本文介绍了WatchService for Java 6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 7引入了 WatchService 来持续监视文件系统。有没有Java 6的后端?



是否有纯Java库具有相似的功能?

解决方案

是的,当然。 Apache VFS正是这样做的。你可以在 http://commons.apache.org/vfs/ 找到它。它是一个纯Java库,可以监视文件,使用起来非常简单:

  FileSystemManager manager = VFS.getManager(); 
FileObject file = manager.resolveFile(c:/MyFile.txt);

DefaultFileMonitor fm = new DefaultFileMonitor(new MyListener());
fm.setDelay(5000);
fm.addFile(file);
fm.start();

上面的代码会监视文件c:/MyFile.txt。如果它改变,则调用对象new MyListener()。


Java 7 introduced WatchService for monitoring file systems continuously. Is there a backport for Java 6 ?

Are there pure Java libraries with similar features ?

解决方案

yes, of course. Apache VFS does exactly this. you can find it under http://commons.apache.org/vfs/. It's a pure java library that can monitor files and it's pretty easy to use:

FileSystemManager manager = VFS.getManager();
FileObject file= manager.resolveFile("c:/MyFile.txt");

DefaultFileMonitor fm = new DefaultFileMonitor(new MyListener());
fm.setDelay(5000);
fm.addFile(file); 
fm.start();

the code above will monitor the file c:/MyFile.txt. if it changes, the object new MyListener() is called.

这篇关于WatchService for Java 6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆