RSS
热门关键字:  java  Ajax  JSP  JSF  Struts
当前位置 : 首页>Java>列表

采用多线程自动监视并扫描指定文件夹中的文件变化

来源: 作者: 时间:2007-09-29 点击:
listenerList = new ArrayListFileListener();

private File dir;
private long interval;
private long lastScanedTime;
private int oldLength;

public void addListener(FileListener listener) {
listenerList.add(listener);
}

public void start(boolean isDamon) {
Thread t = new Thread(this);
t.setDaemon(isDamon);
t.start();
}

public FileMonitor(String _dir, long intervalTime) {
File f = new File(_dir);
if(!f.isDirectory())
throw new RuntimeException();

//
筛选出不是标准文件的目录

File[] fileList = f.listFiles();
for( int i = 0; i
fileList.length; i++ ) {
if(fileList[i].isDirectory()) {
fileList[i].delete();
}
}

this.dir = f;
this.interval = intervalTime ;
this.oldLength = dir.listFiles().length;
}

public File[] getMonitoredFile() {
return this.dir.listFiles();
}

public void run() {
while(true) {
if(!dir.isDirectory())
throw new RuntimeException();
if(filesChanged()) {
fireFilesChangedEvent(this);
}
try {
Thread.sleep(interval);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}

private void fireFilesChangedEvent(FileMonitor monitor) {
for( int i = 0; i
listenerList.size(); i ++ ) {
listenerList.get(i).dirChanged(monitor);
}
}

protected boolean filesChanged() {
File[] newFiles = dir.listFiles();
if(newFiles.length != oldLength) {
oldLength = newFiles.length;
//System.out.println(newFiles.length);
return true;
}
for( int i = 0; i

最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册