Atitit java文件监控 WatchService 的使用
目录
4.1.1. vb.net 用FileSystemWatcher监视文件变化,文件变化时如..._百度知道 2
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs) throws IOException {
dir.register(watchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE);
return FileVisitResult.CONTINUE;
}
});
Path curdir = (Path) WatchKey1.watchable();
Path Path1_newDir = curdir.resolve(fileName);
if (Files.isDirectory(Path1_newDir)) {
// 注意watchEvent.context()这个只有一个文件名(坑爹啊 为啥不给全路径)
Path1_newDir.register(watchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE);
}
}
// 重置其关联的监控器
WatchKey1.reset();
}
}
1个回答 - 回答时间: 2013年9月3日
最佳答案: Private WithEvents fw As IO.FileSystemWatcher Private Shared Sub OnChanged(source As Object, e As IO.FileSystemEventArgs) Handles f
2019/1/1 14:03:52
常清芮 2019/1/1 14:03:52
package com.censoft;
import java.io
.File;
import java.io
.FileFilter;
import java.io
.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.nio.file.attribute.BasicFileAttributes;
import org.apache.commons.io
.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;
public class filemonitor {
public static void main(String[] args) throws InterruptedException,
IOException {
String filePath = "d:\\filenew";
watchFileMethod(filePath, new customProcess() {
public void process(String fname) {
System.out.println(fname);
}
});
}
private static void watchFileMethod(String filePath,
customProcess customProcess) throws IOException,
InterruptedException {
Log log = LogFactory.getLog(WatchFilePathTask.class);
// 获取监控服务
// 可同时获取多个监控服务
final WatchService watchService = FileSystems.getDefault()
.newWatchService();
log.debug("获取监控服务" + watchService);
FileUtils.forceMkdir(new File(filePath));
Path path = FileSystems.getDefault().getPath(filePath);
log.debug("@@@:Path:" + path);
// BasicFileAttributes
// 处理下级多层目录
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs) throws IOException {
dir.register(watchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE);
return FileVisitResult.CONTINUE;
}
});
// 注册监控服务,监控新增事件
// 将指定的监控器注册给Path对象所代表的文件节点,register方法返回的就是节点的监控池
/**
* p.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY,
* StandardWatchEventKinds.ENTRY_DELETE,
* StandardWatchEventKinds.ENTRY_CREATE);
*/
WatchKey WatchKey1 = path.register(watchService,
StandardWatchEventKinds.ENTRY_CREATE);
while (true) {
// 尝试获取下一个变化信息,如果没有则一直等待
// 长时间一直监控需要用take,如果是指定时间监控则用poll
WatchKey1 = watchService.take();
for (WatchEvent<?> WatchEvent1 : WatchKey1.pollEvents()) { // 获取事件列表
// System.out.println("["+path+"/"+event.context()+"]文件发生了["+event.kind()+"]事件");
// 检查文件名是否符合要求
Path curdir1 = (Path) WatchKey1.watchable();
// 获取目录下新增的文件名
//
String fileName = WatchEvent1.context().toString();
String filePath2 =curdir1.toFile().getAbsolutePath().toString()
+ File.separator + fileName;
// log.info
("import filePath:"+filePath2);
customProcess.process(filePath2);
// 处理新目录
Path curdir = (Path) WatchKey1.watchable();
Path Path1_newDir = curdir.resolve(fileName);
if (Files.isDirectory(Path1_newDir)) {
// 注意watchEvent.context()这个只有一个文件名(坑爹啊 为啥不给全路径)
Path1_newDir.register(watchService,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE);
}
}
// 重置其关联的监控器
WatchKey1.reset();
}
}
}