2011-05-21 95 views
1

我想要監視多個文件夾是否在文件夾中添加新文件。 如果文件被添加到文件夾中,我想獲取他的文件名稱。 如何做到這一點。在java中,如何看多個目錄

+1

http://stackoverflow.com/questions/1096404/is-there-a-sophisticated-file-system-monitor-for-java-which-is-的重複freeware-or-open -s和http://stackoverflow.com/questions/3810790/is-it-possible-to-monitor-folder-using-java-code – 2011-05-21 08:50:38

回答

1

請試試這個,

for(;;){ 

    System.out.println("START MONITORING **************"); 


    Path faxFolder = Paths.get("E:\\activiti\\monitor\\m1"); 
    Path faxFolder2 = Paths.get("E:\\activiti\\monitor\\m2"); 
    WatchService watchService = FileSystems.getDefault().newWatchService(); 
    faxFolder.register(watchService, StandardWatchEventKinds.ENTRY_CREATE); 
    faxFolder2.register(watchService, StandardWatchEventKinds.ENTRY_CREATE); 


    boolean valid = true; 
    WatchKey watchKey = watchService.take(); 
    for (WatchEvent<?> event : watchKey.pollEvents()) { 
     WatchEvent.Kind kind = event.kind(); 
     if (StandardWatchEventKinds.ENTRY_CREATE.equals(event.kind())) { 
      String fileName = event.context().toString(); 
      System.out.println(fileName); 

     } 
    } 



} 
相關問題