2011-05-21 67 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); 

     } 
    } 



}