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 {

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

	}

2、applicationContext.xml添加配置

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



    	
    	

至此完成启动项目,在指定文件夹下增加、修改、删除文件都会触发相关方法

你可能感兴趣的:(WEB,JAVA)