Monitor Directories and File(II)Timer and commons-vfs

本文介绍了一种使用Java实现文件及子目录变化监听的方法。通过自定义监听器接口和任务类,实现了对指定文件修改时间的监测,并利用Timer进行周期性的检查。此外,还介绍了使用commons-vfs库来实现更复杂的监听需求。
Monitor Directories and File(II)Timer and commons-vfs

1. Timer and TimerTask
The Interface FileChangeListener.java:
package com.xxxx.importdata.filemonitor.timer;
public interface FileChangeListener
{
public void fileChanged(String filename);
}
The ClassFileChangeListener.java:
package com.xxxxx.importdata.filemonitor.timer;
public class ClassFileChangeListener implements FileChangeListener {
public void fileChanged(String filename) {
System.out
.println("File " + filename + " modified ,it must reload !");
}
}
The most import class FileMonitor.java:
package com.xxxxx.importdata.filemonitor.timer;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
public class FileMonitor
{
private static final FileMonitor instance = new FileMonitor();
private Timer timer;
private Map<String, FileMonitorTask> timerEntries;
private FileMonitor()
{
this.timerEntries = new HashMap<String, FileMonitorTask>();
this.timer = new Timer();
}
public static FileMonitor getInstance()
{
return instance;
}
public void addFileChangeListener(FileChangeListener listener, String filename, long period)
{
this.removeFileChangeListener(filename);
FileMonitorTask task = new FileMonitorTask(listener, filename);
this.timerEntries.put(filename, task);
// schedule(TimerTask task,long delay,long period)
// schedule(TimerTask task,long delay)
// schedule(TimerTask task,Date time)
// schedule(TimerTask task,Date firstTime,long period)
this.timer.scheduleAtFixedRate(task, period, period);
}
public void removeFileChangeListener(String filename)
{
FileMonitorTask task = (FileMonitorTask) this.timerEntries.remove(filename);
if (task != null)
{
task.cancel();
}
}
private static class FileMonitorTask extends TimerTask
{
private FileChangeListener listener;
private String filename;
private File monitoredFile;
private long lastModified;
public FileMonitorTask(FileChangeListener listener, String filename)
{
this.listener = listener;
this.filename = filename;
this.monitoredFile = new File(filename);
if (!this.monitoredFile.exists())
{
return;
}
this.lastModified = this.monitoredFile.lastModified();
}
public void run()
{
long latestChange = this.monitoredFile.lastModified();
if (this.lastModified != latestChange)
{
this.lastModified = latestChange;
this.listener.fileChanged(this.filename);
}
}
}
public static void main(String[] args)
{
String path = "/home/luohua/tmp/t1.txt";
//String path = "/home/luohua/life/work/xxxxx/xxxx-gen/records/";
FileMonitor.getInstance().addFileChangeListener(new ClassFileChangeListener(), path, 2000);
}
}

I am using Linux system, I notice that this lastModified can only detect the file changes. But if there are some changes in the
sub directory. It can not detect that.

And one more thing, it is said that the Timer will cost a lot. I will check if there is another better way.

2. commons-vfs
ivy.xml:
<dependency org="commons-vfs" name="commons-vfs" rev="1.0" />

the test java class is VFSMonitor.java:
package com.xxxxx.importdata.filemonitor.vfs;
import org.apache.commons.vfs.FileChangeEvent;
import org.apache.commons.vfs.FileListener;
import org.apache.commons.vfs.FileName;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileSystemManager;
import org.apache.commons.vfs.VFS;
import org.apache.commons.vfs.impl.DefaultFileMonitor;
public class VFSMonitor
{
private static final String PATH = "/home/luohua/life/work/xxxxx/xxxx-gen/records/";
public static void main(String[] args)
{
FileSystemManager fsManager = null;
FileObject listendir = null;
try
{
fsManager = VFS.getManager();
listendir = fsManager.resolveFile(PATH);
}
catch (FileSystemException e)
{
e.printStackTrace();
}
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 + " has changed.");
}
});
fm.setRecursive(true);
fm.addFile(listendir);
fm.start();
try
{
Thread.sleep(1000000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
fm.stop();
}
}

references:
http://www.iteye.com/topic/1096698
http://www.blogjava.net/quaff/archive/2006/03/02/33229.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值