1. 程式人生 > >JAVA 專案監聽檔案是否發生變化

JAVA 專案監聽檔案是否發生變化

1、Spring容器都初始化完成之後做操作

	package com.luwen.dai;

	import org.springframework.beans.factory.annotation.Autowired;
	import org.springframework.context.ApplicationListener;
	import org.springframework.context.event.ContextRefreshedEvent;

	public class ContextFileListener implements ApplicationListener<ContextRefreshedEvent> {

		@Autowired
		private FileMonitor fileMonitor;
		
		@Override
		public void onApplicationEvent(ContextRefreshedEvent event) {
			
				try {
					fileMonitor.monitor();
					fileMonitor.start();
				} catch (Exception e) {
					e.printStackTrace();
				}
		}

	}

2、applicationContext.xml新增配置
<bean class="com.luwen.dai.ContextFileListener"/>
3、新增檔案監聽 FileMonitor、FileListener 可看資料 commons-io-2.4
package com.luwen.dai;

import java.io.File;

import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;

public class FileMonitor {
	
	private FileAlterationMonitor monitor = null;
	
	private String filePath;
	private FileAlterationListener alterationListener;
	private long intervalTime = 10000L;
	
    public FileMonitor(long interval) throws Exception {  
        monitor = new FileAlterationMonitor(interval);  
    }
    public FileMonitor() throws Exception {  
        monitor = new FileAlterationMonitor(intervalTime);  
    }
    
    public void monitor(String path, FileAlterationListener listener) {  
        FileAlterationObserver observer = new FileAlterationObserver(new File(path));  
        monitor.addObserver(observer);  
        observer.addListener(listener);  
    }
    public void monitor() {  
        FileAlterationObserver observer = new FileAlterationObserver(new File(filePath));  
        monitor.addObserver(observer);  
        observer.addListener(alterationListener);  
    }
    public void stop() throws Exception{  
        monitor.stop();  
    }  
    public void start() throws Exception {  
        monitor.start();  
    }
    
    

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public FileAlterationListener getAlterationListener() {
		return alterationListener;
	}

	public void setAlterationListener(FileAlterationListener alterationListener) {
		this.alterationListener = alterationListener;
	}
	public long getIntervalTime() {
		return intervalTime;
	}
	public void setIntervalTime(long intervalTime) {
		this.intervalTime = intervalTime;
	}
	
}

package com.luwen.dai;

import java.io.File;

import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationObserver;

public class FileListener implements FileAlterationListener {

	@Override
	public void onStart(FileAlterationObserver observer) {
		// TODO Auto-generated method stub
		System.out.println("onStart()");

	}

	@Override
	public void onDirectoryCreate(File directory) {
		// TODO Auto-generated method stub
		System.out.println("onDirectoryCreate()");
	}

	@Override
	public void onDirectoryChange(File directory) {
		// TODO Auto-generated method stub
		System.out.println("onDirectoryChange()");
	}

	@Override
	public void onDirectoryDelete(File directory) {
		// TODO Auto-generated method stub
		System.out.println("onDirectoryDelete()");
	}

	@Override
	public void onFileCreate(File file) {
		// TODO Auto-generated method stub
		System.out.println("onFileCreate()");
	}

	@Override
	public void onFileChange(File file) {
		// TODO Auto-generated method stub
		System.out.println("onFileChange()");
	}

	@Override
	public void onFileDelete(File file) {
		// TODO Auto-generated method stub
		System.out.println("onFileDelete()");
	}

	@Override
	public void onStop(FileAlterationObserver observer) {
		// TODO Auto-generated method stub
		System.out.println("onStop()");
	}

}

4、在applicationContext.xml 配置相關Bean
<bean id="fileListener" class="com.luwen.dai.FileListener" />
<bean id="fileMonitor" class="com.luwen.dai.FileMonitor">
    	<property name="alterationListener" ref="fileListener"/>
    	<property name="filePath" value="H:/SVN-"/>
</bean>

至此完成啟動專案,在指定資料夾下增加、修改、刪除檔案都會觸發相關方法