i want to monitor the multiple folders whether new files are added in the folders. if files are added in folder,i want to get he name of the file. How to do this.
Asked
Active
Viewed 2,412 times
1
-
1Duplicate of http://stackoverflow.com/questions/1096404/is-there-a-sophisticated-file-system-monitor-for-java-which-is-freeware-or-open-s and http://stackoverflow.com/questions/3810790/is-it-possible-to-monitor-folder-using-java-code – Nicolas Bousquet May 21 '11 at 08:50
2 Answers
2
Theres a component called File Monitor in the apache commons IO library. I Think it's just what you're looking for.

yurib
- 8,043
- 3
- 30
- 55
1
please try this ,
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);
}
}
}

Jamsheer
- 3,673
- 3
- 29
- 57