2016-12-02 25 views
0

我正在做一個程序來監視文件夾中的文件更改,this是我的程序的參考鏈接。但不幸的是我面臨的一些錯誤我的代碼

這是我的編譯器錯誤消息顯示

WatchService無法在Java中註冊

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) 



這是我的示例代碼

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) 
     { 

     } 

    } 
} 



我獲取我的註冊方法中的錯誤WatchService,我有搜索一些鏈接墨水我使用語法的方式應該是正確的。

回答

2

我認爲你錯位了括號。試試這個:

keyMap.put(path.register(svc, 
          StandardWatchEventKinds.ENTRY_CREATE, 
          StandardWatchEventKinds.ENTRY_DELETE, 
          StandardWatchEventKinds.ENTRY_MODIFY), 
      path); 
+0

哦,哈哈,我的錯。感謝您的幫助,這是我把它放到正確的地方後的工作 – attack