List of usage examples for java.nio.file FileSystem newWatchService
public abstract WatchService newWatchService() throws IOException;
From source file:Test.java
public static void main(String[] args) throws Exception { FileSystem fileSystem = FileSystems.getDefault(); WatchService watchService = fileSystem.newWatchService(); Path directory = Paths.get("/home/docs"); WatchEvent.Kind<?>[] events = { StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY }; directory.register(watchService, events); while (true) { System.out.println("Waiting for a watch event"); WatchKey watchKey = watchService.take(); System.out.println("Path being watched: " + watchKey.watchable()); if (watchKey.isValid() == false) { return; }//from w ww . j a va 2 s .c om for (WatchEvent<?> event : watchKey.pollEvents()) { System.out.println("Kind: " + event.kind()); System.out.println("Context: " + event.context()); System.out.println("Count: " + event.count()); System.out.println(); } boolean valid = watchKey.reset(); System.out.println(valid); } }
From source file:Main.java
public static void main(String[] args) throws Exception { FileSystem fileSystem = FileSystems.getDefault(); WatchService watchService = fileSystem.newWatchService(); Path directory = Paths.get("c:/"); WatchEvent.Kind<?>[] events = { StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY }; directory.register(watchService, events); while (true) { System.out.println("Waiting for a watch event"); WatchKey watchKey = watchService.take(); System.out.println("Path being watched: " + watchKey.watchable()); System.out.println();/*w w w . j av a 2s . co m*/ if (watchKey.isValid()) { for (WatchEvent<?> event : watchKey.pollEvents()) { System.out.println("Kind: " + event.kind()); System.out.println("Context: " + event.context()); System.out.println("Count: " + event.count()); System.out.println(); } boolean valid = watchKey.reset(); if (!valid) { // The watchKey is not longer registered } } } }
From source file:Main.java
public static void main(String[] args) throws Exception { FileSystem fileSystem = FileSystems.getDefault(); WatchService watchService = fileSystem.newWatchService(); Path directory = Paths.get("c:/"); WatchEvent.Kind<?>[] events = { StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY }; directory.register(watchService, events); while (true) { System.out.println("Waiting for a watch event"); WatchKey watchKey = watchService.take(); System.out.println("Path being watched: " + watchKey.watchable()); System.out.println();/*from w w w. ja v a2s . c om*/ if (watchKey.isValid()) { for (WatchEvent<?> event : watchKey.pollEvents()) { System.out.println("Kind: " + event.kind()); System.out.println("Context: " + event.context()); System.out.println("Count: " + event.count()); System.out.println(); } boolean valid = watchKey.reset(); if (!valid) { // The watchKey is not longer registered } } } }
From source file:net.mindengine.dashserver.compiler.GlobalAssetsFileWatcher.java
@Override public void run() { copyAllAssets();//from www . j a v a 2 s .c o m Path path = Paths.get(this.assetsFolderPath); FileSystem fileSystem = path.getFileSystem(); try (WatchService service = fileSystem.newWatchService()) { path.register(service, ENTRY_MODIFY, ENTRY_CREATE); while (true) { WatchKey watchKey = service.take(); for (WatchEvent<?> watchEvent : watchKey.pollEvents()) { Path watchEventPath = (Path) watchEvent.context(); String fileName = watchEventPath.toString(); copyFileAsset(fileName); } if (!watchKey.reset()) { break; } } } catch (Exception e) { e.printStackTrace(); } }
From source file:acromusashi.stream.ml.common.spout.WatchTextBatchSpout.java
/** * ???????????????????/* w w w. j a v a 2s . c o m*/ * * @param collector Collector * @throws IOException * @throws InterruptedException ? */ @SuppressWarnings({ "rawtypes" }) protected void checkDataFile(TridentCollector collector) throws IOException, InterruptedException { // ????????????????? if (this.isInitialReaded == false) { List<String> fileContents = FileUtils.readLines(this.targetFile); emitTuples(fileContents, collector); this.isInitialReaded = true; // Path dirPath = new File(this.dataFileDir).toPath(); FileSystem fileSystem = dirPath.getFileSystem(); this.watcherService = fileSystem.newWatchService(); this.watchKey = dirPath.register(this.watcherService, new Kind[] { StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY }); return; } // ???????? WatchKey detectedKey = this.watcherService.poll(1, TimeUnit.SECONDS); // ??????????????????????? if (detectedKey == null || detectedKey.equals(this.watchKey) == false) { return; } try { // ??????????????? for (WatchEvent event : detectedKey.pollEvents()) { Path filePath = (Path) event.context(); // ????????????? if (filePath == null || this.targetFile.toPath().getFileName().equals(filePath.getFileName()) == false) { continue; } List<String> fileContents = FileUtils.readLines(this.targetFile); emitTuples(fileContents, collector); } } finally { detectedKey.reset(); } }