java监听文件或者文件夹修改的几种方式

最近由于项目需要,程序在首次运行时会加载配置文件并读取内存,形成一个常量。那么下次配置修改后得重新启动程序进程,会有不少弊端。下面通过搜集,自己整理了下面三种方式:

//1、log4j实现文件内容变化的监听
package com.wxy_beijing;

import org.apache.log4j.helpers.FileWatchdog;

public class Test_Log4jWatchdog
{
    public static void main(String[] args)
    {
        GloablConfig gloablconfig = new GloablConfig("E:\\test\\file\\test.txt");
        gloablconfig.setDelay(10000);
        gloablconfig.start();
        while (true)
        {
            try
            {
                Thread.sleep(10000);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
    
    public static class GloablConfig extends FileWatchdog
    {
        protected GloablConfig(String filename)
        {
            super(filename);
        }
        @Override
        protected void doOnChange()
        {
            System.out.println(filename);
        }
        
    }
}
//2、common-io实现的文件夹变化的监听
package com.wxy_beijing;

import java.io.File;
import java.io.FileFilter;

import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;

public class Test_FileAlterationObserver
{
    
    public static void main(String[] args) throws Exception
    {
        Test_FileAlterationObserver test=new Test_FileAlterationObserver();
        test.test();
    }
    public void test() throws Exception{
        String filePath="E:/test/file/";
        FileFilter filter=FileFilterUtils.and(new MyFileFilter());
        FileAlterationObserver filealtertionObserver=new FileAlterationObserver(filePath, filter);
        filealtertionObserver.addListener(new FileAlterationListenerAdaptor(){

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

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

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

            @Override
            public void onFileCreate(File file)
            {
                System.out.println("onFileCreate"+file.getAbsoluteFile());
                super.onFileCreate(file);
            }

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

            @Override
            public void onStart(FileAlterationObserver observer)
            {
                // TODO Auto-generated method stub
                System.out.println("onStart");
                super.onStart(observer);
            }
            
        });
        
        FileAlterationMonitor filealterationMonitor=new FileAlterationMonitor(1000);
        filealterationMonitor.addObserver(filealtertionObserver);
        filealterationMonitor.start();
    }
    
}
class MyFileFilter implements IOFileFilter{

    @Override
    public boolean accept(File file)
    {
        /*String extension=FilenameUtils.getExtension(file.getAbsolutePath());
        if(extension!=null&&extension.equals("txt")){
            return true;
        }
        return false;*/
        return true;
    }

    @Override
    public boolean accept(File dir, String name)
    {
        return true;
    }
    
}
//3、nio实现文件夹变化的监听
package com.wxy_beijing;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test_ResourceListener
{
    private static ExecutorService fixedThreadPool=Executors.newCachedThreadPool();
    private WatchService ws;
    private String listenerPath;
    private Test_ResourceListener(String path){
        try
        {
            ws=FileSystems.getDefault().newWatchService();
            this.listenerPath=path;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    private void start(){
        fixedThreadPool.execute(new Listner(ws, listenerPath));
    }
    
    public static void addListener(String path) throws IOException{
        Test_ResourceListener resourceListener=new Test_ResourceListener(path);
        Path p=Paths.get(path);
        p.register(resourceListener.ws, StandardWatchEventKinds.ENTRY_MODIFY,StandardWatchEventKinds.ENTRY_DELETE,
            StandardWatchEventKinds.ENTRY_CREATE);
        resourceListener.start();
    }
    
    public static void main(String[] args) throws IOException
    {
        Test_ResourceListener.addListener("E:/test/file/test1/");
        Test_ResourceListener.addListener("E:/test/file/test2/");
    }
    
}




你可能感兴趣的:(java,java,文件,监听)