1.log4j的实现的文件内容变化监听
package com.jp.filemonitor;
import org.apache.log4j.helpers.FileWatchdog;
public class Log4jWatchdog {
public static void main(String[] args) {
GloablConfig gloablConfig = new GloablConfig("D:\\create_lo\\text.txt");
gloablConfig.setDelay(10000);
gloablConfig.start();
while (true) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">class</span> GloablConfig <span style="color: #0000ff;">extends</span><span style="color: #000000;"> FileWatchdog{
</span><span style="color: #0000ff;">protected</span><span style="color: #000000;"> GloablConfig(String filename) {
</span><span style="color: #0000ff;">super</span><span style="color: #000000;">(filename);
}
@Override
</span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> doOnChange() {
System.out.println(filename);
}
}
}
2.common-io实现的文件夹变化的监听
这代码网上很多,可以搜索关键字FileAlterationMonitor,FileAlterationObserver FileAlterationObserver,这样就可以看到示例代码了。
3.nio实现文件夹内容变化的监听
package com.jp.filemonitor;
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.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ResourceListener {
private static ExecutorService fixedThreadPool = Executors.newCachedThreadPool();
private WatchService ws;
private String listenerPath;
private ResourceListener(String path) {
try {
ws = FileSystems.getDefault().newWatchService();
this.listenerPath = path;
start();
} catch (IOException e) {
e.printStackTrace();
}
}
</span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> start() {
fixedThreadPool.execute(</span><span style="color: #0000ff;">new</span> Listner(ws,<span style="color: #0000ff;">this</span><span style="color: #000000;">.listenerPath));
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> addListener(String path) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> IOException {
ResourceListener resourceListener </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> ResourceListener(path);
Path p </span>=<span style="color: #000000;"> Paths.get(path);
p.register(resourceListener.ws,StandardWatchEventKinds.ENTRY_MODIFY,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_CREATE);
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> main(String[] args) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> IOException {
ResourceListener.addListener(</span>"F:\\"<span style="color: #000000;">);
ResourceListener.addListener(</span>"d:\\"<span style="color: #000000;">);
}
}
class Listner implements Runnable {
private WatchService service;
private String rootPath;
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Listner(WatchService service,String rootPath) {
</span><span style="color: #0000ff;">this</span>.service =<span style="color: #000000;"> service;
</span><span style="color: #0000ff;">this</span>.rootPath =<span style="color: #000000;"> rootPath;
}
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> run() {
</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
</span><span style="color: #0000ff;">while</span>(<span style="color: #0000ff;">true</span><span style="color: #000000;">){
WatchKey watchKey </span>=<span style="color: #000000;"> service.take();
List</span><WatchEvent<?>> watchEvents =<span style="color: #000000;"> watchKey.pollEvents();
</span><span style="color: #0000ff;">for</span>(WatchEvent<?><span style="color: #000000;"> event : watchEvents){
</span><span style="color: #008000;">//</span><span style="color: #008000;">TODO 根据事件类型采取不同的操作。。。。。。。</span>
System.out.println("["+rootPath+event.context()+"]文件发生了["+event.kind()+"]事件"+<span style="color: #000000;"> event.count());
}
watchKey.reset();
}
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (InterruptedException e) {
e.printStackTrace();
}</span><span style="color: #0000ff;">finally</span><span style="color: #000000;">{
System.out.println(</span>"fdsfsdf"<span style="color: #000000;">);
</span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
service.close();
} </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) {
e.printStackTrace();
}
}
}
}