java监控文件夹变更_Java监听目录文件变更

Java 7中提供了java.nio.file.WatchService用来监听文件系统目录变更,用起来还是比较简单的,在这里记录一下。

创建一个WatchService

代码如下:

WatchService watcher = FileSystems.getDefault().newWatchService();

当然一个WatchService是关联着操作系统资源的,需要完全的关闭,所以一般像下面这样写:

WatchService watcher = null;

try {

watcher = FileSystems.getDefault().newWatchService();

...

} finally {

if(watcher != null){

try {

watcher.close();

} catch (Exception ignore){}

}

}

注册监听一个目录

Path dir = Paths.get("/somewhere");

WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);

这里这个key即与这个dir关联,以后dir里一旦发生监听的事件,则从watcher就可以poll或take到这个key

循环从WatchService里取出有信号的WatchKey

for (;;) {

// wait for key to be signalled

WatchKey key;

try {

key = watcher.take();

} catch (InterruptedException x) {

return;

}

for (WatchEvent> event: key.pollEvents()) {

WatchEvent.Kind kind = event.kind();

// TBD - provide example of how OVERFLOW event is handled

if (kind == OVERFLOW) {

continue;

}

// Context for directory entry event is the file name of entry

WatchEvent ev = (WatchEvent)event;

Path name = ev.context();

...

}

// reset key and remove from set if directory no longer accessible

boolean valid = key.reset();

if (!valid) {

break;

}

}

WatchKey被cancel或WatchService被close时,key.reset()会返回false, 此时应该跳出循环。

递归监听目录

上述的代码很简单了,跟Java原生NIO的思想差不多。不过经我实验,dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);只会监听该目录下一级的变更事件,子目录下的变更就监听不到了。例如在/somewhere目录下建一个目录test,再在test下建一个文件test.txt,此时就监听不到了。简单写了个递归监听某个目录下所有变更的例子,如下

import java.nio.file.*;

import static java.nio.file.StandardWatchEventKinds.*;

import static java.nio.file.LinkOption.*;

import java.nio.file.attribute.*;

import java.io.*;

import java.util.*;

/**

* Created by jeremy on 16/5/12.

*/

public class WatchDir {

private final WatchService watcher;

private final Map keys;

private boolean trace = false;

@SuppressWarnings("unchecked")

static WatchEvent cast(WatchEvent> event) {

return (WatchEvent)event;

}

/**

* Register the given directory with the WatchService

*/

private void register(Path dir) throws IOException {

WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);

if (trace) {

Path prev = keys.get(key);

if (prev == null) {

System.out.format("register: %s\n", dir);

} else {

if (!dir.equals(prev)) {

System.out.format("update: %s -> %s\n", prev, dir);

}

}

}

keys.put(key, dir);

}

/**

* Register the given directory, and all its sub-directories, with the

* WatchService.

*/

private void registerAll(final Path start) throws IOException {

// register directory and sub-directories

Files.walkFileTree(start, new SimpleFileVisitor() {

@Override

public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)

throws IOException

{

register(dir);

return FileVisitResult.CONTINUE;

}

});

}

/**

* Creates a WatchService and registers the given directory

*/

WatchDir(Path dir) throws IOException {

this.watcher = FileSystems.getDefault().newWatchService();

this.keys = new HashMap();

System.out.format("Scanning %s ...\n", dir);

registerAll(dir);

System.out.println("Done.");

// enable trace after initial registration

this.trace = true;

}

/**

* Process all events for keys queued to the watcher

*/

void processEvents() {

for (;;) {

// wait for key to be signalled

WatchKey key;

try {

key = watcher.take();

} catch (InterruptedException x) {

return;

}

Path dir = keys.get(key);

if (dir == null) {

System.err.println("WatchKey not recognized!!");

continue;

}

for (WatchEvent> event: key.pollEvents()) {

WatchEvent.Kind kind = event.kind();

// TBD - provide example of how OVERFLOW event is handled

if (kind == OVERFLOW) {

continue;

}

// Context for directory entry event is the file name of entry

WatchEvent ev = cast(event);

Path name = ev.context();

Path child = dir.resolve(name);

// print out event

System.out.format("%s: %s\n", event.kind().name(), child);

// if directory is created, and watching recursively, then

// register it and its sub-directories

if (kind == ENTRY_CREATE) {

try {

if (Files.isDirectory(child, NOFOLLOW_LINKS)) {

registerAll(child);

}

} catch (IOException x) {

// ignore to keep sample readbale

}

}

}

// reset key and remove from set if directory no longer accessible

boolean valid = key.reset();

if (!valid) {

keys.remove(key);

// all directories are inaccessible

if (keys.isEmpty()) {

break;

}

}

}

}

static void usage() {

System.err.println("usage: java WatchDir dir");

System.exit(-1);

}

public static void main(String[] args) throws IOException {

// parse arguments

if (args.length == 0 || args.length > 1)

usage();

// register directory and process its events

Path dir = Paths.get(args[0]);

new WatchDir(dir).processEvents();

}

}

Path name = ev.context();拿到的仅仅只是相对于dir的Path,并不是绝对路径,为了拼出绝对路径,没办法只能建了一个Map keys,用来维护WatchKey与dir的映射关系。本以为这样写,面对一个巨大的目录,Map keys将会很大,性能不好。实测监听一个100多G的目录,并没占用太多内存,进程使用的文件句柄数也正常得很,而且性能还比较高。

希望Java以后的版本能直接在WatchEvent拿到变更ENTRY的绝对路径就好了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值