/** Check that a cancelled key will never be queued */ static void testCancel(Path dir) throws IOException { System.out.println("-- Cancel --"); try (WatchService watcher = FileSystems.getDefault().newWatchService()) { System.out.format("register %s for events\n", dir); WatchKey myKey = dir.register(watcher, new WatchEvent.Kind<?>[] {ENTRY_CREATE}); checkKey(myKey, dir); System.out.println("cancel key"); myKey.cancel(); // create a file in the directory Path file = dir.resolve("mars"); System.out.format("create: %s\n", file); Files.createFile(file); // poll for keys - there will be none System.out.println("poll..."); try { WatchKey key = watcher.poll(3000, TimeUnit.MILLISECONDS); if (key != null) throw new RuntimeException("key should not be queued"); } catch (InterruptedException x) { throw new RuntimeException(x); } // done Files.delete(file); System.out.println("OKAY"); } }
public ExerciseDAO(String fileName) { this.fileName = fileName; this.myList = new ArrayList<>(); try { Files.createFile(Paths.get(fileName)); } catch (FileAlreadyExistsException fae) { ; } catch (IOException ioe) { System.out.println("Create file error with " + ioe.getMessage()); } readList(); }
private void initFrame() { this.setLocationRelativeTo(null); try { if (!Files.exists(saveName)) Files.createFile(saveName); laden(saveName); } catch (IOException e) { e.printStackTrace(); } nUebSchr.setSelected(true); if (!quellListModel.isEmpty()) quellJList.setSelectedIndex(0); if (!zielListModel.isEmpty()) zielJList.setSelectedIndex(0); starteLaufwerkspruefung(); }
/** * This method tries to write an empty file and moves it using an atomic move operation. This * method throws an {@link IllegalStateException} if this operation is not supported by the * filesystem. This test is executed on each of the data directories. This method cleans up all * files even in the case of an error. */ public void ensureAtomicMoveSupported() throws IOException { final NodePath[] nodePaths = nodePaths(); for (NodePath nodePath : nodePaths) { assert Files.isDirectory(nodePath.path) : nodePath.path + " is not a directory"; final Path src = nodePath.path.resolve("__es__.tmp"); Files.createFile(src); final Path target = nodePath.path.resolve("__es__.final"); try { Files.move(src, target, StandardCopyOption.ATOMIC_MOVE); } catch (AtomicMoveNotSupportedException ex) { throw new IllegalStateException( "atomic_move is not supported by the filesystem on path [" + nodePath.path + "] atomic_move is required for elasticsearch to work correctly.", ex); } finally { Files.deleteIfExists(src); Files.deleteIfExists(target); } } }
/** Simple test of each of the standard events */ static void testEvents(Path dir) throws IOException { System.out.println("-- Standard Events --"); FileSystem fs = FileSystems.getDefault(); Path name = fs.getPath("foo"); try (WatchService watcher = fs.newWatchService()) { // --- ENTRY_CREATE --- // register for event System.out.format("register %s for ENTRY_CREATE\n", dir); WatchKey myKey = dir.register(watcher, new WatchEvent.Kind<?>[] {ENTRY_CREATE}); checkKey(myKey, dir); // create file Path file = dir.resolve("foo"); System.out.format("create %s\n", file); Files.createFile(file); // remove key and check that we got the ENTRY_CREATE event takeExpectedKey(watcher, myKey); checkExpectedEvent(myKey.pollEvents(), StandardWatchEventKinds.ENTRY_CREATE, name); System.out.println("reset key"); if (!myKey.reset()) throw new RuntimeException("key has been cancalled"); System.out.println("OKAY"); // --- ENTRY_DELETE --- System.out.format("register %s for ENTRY_DELETE\n", dir); WatchKey deleteKey = dir.register(watcher, new WatchEvent.Kind<?>[] {ENTRY_DELETE}); if (deleteKey != myKey) throw new RuntimeException("register did not return existing key"); checkKey(deleteKey, dir); System.out.format("delete %s\n", file); Files.delete(file); takeExpectedKey(watcher, myKey); checkExpectedEvent(myKey.pollEvents(), StandardWatchEventKinds.ENTRY_DELETE, name); System.out.println("reset key"); if (!myKey.reset()) throw new RuntimeException("key has been cancalled"); System.out.println("OKAY"); // create the file for the next test Files.createFile(file); // --- ENTRY_MODIFY --- System.out.format("register %s for ENTRY_MODIFY\n", dir); WatchKey newKey = dir.register(watcher, new WatchEvent.Kind<?>[] {ENTRY_MODIFY}); if (newKey != myKey) throw new RuntimeException("register did not return existing key"); checkKey(newKey, dir); System.out.format("update: %s\n", file); try (OutputStream out = Files.newOutputStream(file, StandardOpenOption.APPEND)) { out.write("I am a small file".getBytes("UTF-8")); } // remove key and check that we got the ENTRY_MODIFY event takeExpectedKey(watcher, myKey); checkExpectedEvent(myKey.pollEvents(), StandardWatchEventKinds.ENTRY_MODIFY, name); System.out.println("OKAY"); // done Files.delete(file); } }
/** * Test that directory can be registered with more than one watch service and that events don't * interfere with each other */ static void testTwoWatchers(Path dir) throws IOException { System.out.println("-- Two watchers test --"); FileSystem fs = FileSystems.getDefault(); WatchService watcher1 = fs.newWatchService(); WatchService watcher2 = fs.newWatchService(); try { Path name1 = fs.getPath("gus1"); Path name2 = fs.getPath("gus2"); // create gus1 Path file1 = dir.resolve(name1); System.out.format("create %s\n", file1); Files.createFile(file1); // register with both watch services (different events) System.out.println("register for different events"); WatchKey key1 = dir.register(watcher1, new WatchEvent.Kind<?>[] {ENTRY_CREATE}); WatchKey key2 = dir.register(watcher2, new WatchEvent.Kind<?>[] {ENTRY_DELETE}); if (key1 == key2) throw new RuntimeException("keys should be different"); // create gus2 Path file2 = dir.resolve(name2); System.out.format("create %s\n", file2); Files.createFile(file2); // check that key1 got ENTRY_CREATE takeExpectedKey(watcher1, key1); checkExpectedEvent(key1.pollEvents(), StandardWatchEventKinds.ENTRY_CREATE, name2); // check that key2 got zero events WatchKey key = watcher2.poll(); if (key != null) throw new RuntimeException("key not expected"); // delete gus1 Files.delete(file1); // check that key2 got ENTRY_DELETE takeExpectedKey(watcher2, key2); checkExpectedEvent(key2.pollEvents(), StandardWatchEventKinds.ENTRY_DELETE, name1); // check that key1 got zero events key = watcher1.poll(); if (key != null) throw new RuntimeException("key not expected"); // reset for next test key1.reset(); key2.reset(); // change registration with watcher2 so that they are both // registered for the same event System.out.println("register for same event"); key2 = dir.register(watcher2, new WatchEvent.Kind<?>[] {ENTRY_CREATE}); // create file and key2 should be queued System.out.format("create %s\n", file1); Files.createFile(file1); takeExpectedKey(watcher2, key2); checkExpectedEvent(key2.pollEvents(), StandardWatchEventKinds.ENTRY_CREATE, name1); System.out.println("OKAY"); } finally { watcher2.close(); watcher1.close(); } }