[英]WatchService not able to register in Java
i am doing a program for monitoring the file change in a folder, this is my reference link for my program. 我正在做一个程序来监视文件夹中的文件更改, 这是我程序的参考链接。 But unfortunately i am facing some error with my code
但是不幸的是我的代码遇到了一些错误
This is the error message show in my compiler 这是我的编译器中显示的错误消息
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method register(WatchService, WatchEvent.Kind<?>[], WatchEvent.Modifier...) in the type Path is not applicable for the arguments (WatchService, WatchEvent.Kind<Path>, WatchEvent.Kind<Path>, WatchEvent.Kind<Path>, Path)
This is my sample code 这是我的示例代码
import java.nio.file.FileSystem;
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.HashMap;
import java.util.Map;
public class FileDetect {
public static void main(String [] args)
{
try(WatchService svc = FileSystems.getDefault().newWatchService())
{
Map<WatchKey, Path> keyMap = new HashMap<>();
Path path = Paths.get("files");
keyMap.put(path.register(svc,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY,
path));
WatchKey wk ;
do
{
wk = svc.take();
Path dir = keyMap.get(wk);
for(WatchEvent<?> event : wk.pollEvents())
{
WatchEvent.Kind<?> type = event.kind();
Path fileName = (Path)event.context();
System.out.print(fileName);
}
}
while(wk.reset());
}
catch(Exception e)
{
}
}
}
I am get the error in my register method for the WatchService , i have search some link which i think the way i use the syntax should be correct. 我在WatchService的注册方法中遇到错误,我搜索了一些链接,我认为我使用语法的方式应该是正确的。
I think you misplaced the parenthesis. 我认为您放错了括号。 Try this:
尝试这个:
keyMap.put(path.register(svc,
StandardWatchEventKinds.ENTRY_CREATE,
StandardWatchEventKinds.ENTRY_DELETE,
StandardWatchEventKinds.ENTRY_MODIFY),
path);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.