我要连续监视文件夹(例如C:/ Users / me / rootFolder)中的内容是否有任何更改-
我已使用java.nio包中的监视服务,并且能够获取根文件夹的结果。但是我们如何在整个目录树中使用监视服务?
此外,我正在寻找一种使用时间戳而不是watchservice进行此操作的方法,因为这是客户的要求。
这是我为根目录编写的代码。
public void watchServices() throws IOException,
InterruptedException
{
destFolder = LoginController.destFolder;
logger.info("Started Watching Folder");
Path watchFolder = Paths.get(sourceFolder);
WatchService watchService = FileSystems.getDefault().newWatchService();
watchFolder.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
boolean valid = true;
do {
WatchKey watchKey = watchService.take();
for (WatchEvent event : watchKey.pollEvents()) {
WatchEvent.Kind kind = event.kind();
if (StandardWatchEventKinds.ENTRY_CREATE.equals(kind))
{
String fileName = event.context().toString();
File file = new File(watchFolder + "\\" + fileName);
if (file.isFile())
{
logger.info("uploading file to cdi-dms server: " +
file.getAbsolutePath());
uploadFile(file,destFolder);
}
else if(file.isDirectory())
{
logger.info("creating directory in cdi-dms server: "
+ file.getAbsolutePath());
createDirectory(file,destFolder);
destFolder = destFolder+file.getName()+"/";
sync(file);
}
}
}
valid = watchKey.reset();
}while (valid);
}