public static void main(String[] args) { try { WatchService watcher = FileSystems.getDefault().newWatchService(); Path dir = Paths.get("D:\\take it"); dir.register(watcher, ENTRY_CREATE, ENTRY_MODIFY); System.out.println("Watch Service registered for dir: " + dir.getFileName()); System.out.println("How many files are already there " + folder.getAbsolutePath()); listFilesForFolder(folder); while (true) { WatchKey key; try { key = watcher.take(); } catch (InterruptedException ex) { return; } for (WatchEvent<?> event : key.pollEvents()) { WatchEvent.Kind<?> kind = event.kind(); @SuppressWarnings("unchecked") WatchEvent<Path> ev = (WatchEvent<Path>) event; Path fileName = ev.context(); // String p = folder.getAbsolutePath()+ "\\" +fileName; if ((fileName.toString()).contains(".")) { System.out.println("snadkf"); } else { System.out.println(kind.name() + ": " + fileName); } // System.out.println("New file has been arrived: "+fileName); } /*if (kind == ENTRY_MODIFY && fileName.toString().equals("DirectoryWatchDemo.java")) { System.out.println("My source file has changed!!!"); } }*/ boolean valid = key.reset(); if (!valid) { break; } } } catch (IOException ex) { System.err.println(ex); } }
void scanDirectory(String path) throws IOException, InterruptedException { watcher = FileSystems.getDefault().newWatchService(); Path directoryName = null; Path dir = Paths.get(path); // dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY); registerAll(dir); System.out.println("er i path"); for (; ; ) { WatchKey key; try { key = watcher.take(); } catch (InterruptedException e) { e.printStackTrace(); return; } // System.out.println("er i while"); for (WatchEvent<?> event : key.pollEvents()) { WatchEvent.Kind kind = event.kind(); WatchEvent<Path> ev = (WatchEvent<Path>) event; Path filename = ev.context(); Path directory; if (filename != null) { // System.out.println("filename != null."); directory = dir.resolve(filename); } else { continue; } // System.out.println("filename er "+filename); if (kind == OVERFLOW) { // System.out.println("fikk en overflow. "); continue; } else if (kind == ENTRY_MODIFY) { // System.out.println(kind.name() + " for path/directory " + directory); } else if (kind == ENTRY_CREATE) { System.out.println(kind.name() + " for path/directory " + directory); // System.out.println("suffix length er "+suffix[1]); System.out.println(kind.name() + " for path/directory " + directory); // System.out.println("filnavn er" + filename); String suffix[] = (directory.toString()).split("\\."); if ((suffix.length > 1) && (suffix[1].endsWith("evt"))) { System.out.println("Laget fil."); String adress = (directory.getParent().toAbsolutePath() + "/" + directoryName + "/" + filename); convertToSimpleEvent(adress); } else if (Files.isDirectory(directory, LinkOption.NOFOLLOW_LINKS)) { directoryName = filename; registerAll(directory); // System.out.println("Laget fil og venter i 6 sec."); Thread.sleep(6000); // traverseDirectories(directory.toString()); // System.out.println("Ny mappe er laget på lokajson."+directory); } } else if (kind == ENTRY_DELETE) { System.out.println(kind.name() + " " + directory); } } boolean valid = key.reset(); if (!valid) { System.out.println("ble ikke valid " + valid); keys.remove(key); // all directories are inaccessible if (keys.isEmpty()) { break; } } } }
void processEvents() { for (; ; ) { // wait for key to be signaled WatchKey key; try { key = watcher.take(); } catch (InterruptedException x) { return; } Path dir = keys.get(key); if (dir == null) { System.err.println("WatchKey not recognized!!"); continue; } for (WatchEvent<?> event : key.pollEvents()) { WatchEvent.Kind<?> kind = event.kind(); // System.out.println("Event: " + kind.name()); // This key is registered only // for ENTRY_CREATE events, // but an OVERFLOW event can // occur regardless if events // are lost or discarded. if (kind == StandardWatchEventKinds.OVERFLOW) { System.out.println("Overflow"); continue; } else if (kind == StandardWatchEventKinds.ENTRY_CREATE) { // The filename is the // context of the event. WatchEvent<Path> ev = (WatchEvent<Path>) event; Path filename = ev.context(); // // Context for directory entry event is the file name of entry // WatchEvent<Path> ev = cast(event); // Path name = ev.context(); Path child = dir.resolve(filename); // print out event // System.out.format("%s: %s\n", event.kind().name(), child); handleFileCreated(kind, filename, dir); } else if (kind == StandardWatchEventKinds.ENTRY_DELETE) { WatchEvent<Path> ev = (WatchEvent<Path>) event; Path filename = ev.context(); // Context for directory entry event is the file name of entry Path child = dir.resolve(filename); // print out event System.out.format("Removed! %s: %s\n", event.kind().name(), child); handleFileDeleted(kind, filename, dir); } else { System.out.println("Other event: " + kind.name()); } // reset key and remove from set if directory no longer accessible boolean valid = key.reset(); if (!valid) { keys.remove(key); // all directories are inaccessible if (keys.isEmpty()) { System.out.println("Good bye!"); break; } } } } }