當前位置: 首頁>>代碼示例>>Java>>正文


Java WatchService.take方法代碼示例

本文整理匯總了Java中java.nio.file.WatchService.take方法的典型用法代碼示例。如果您正苦於以下問題:Java WatchService.take方法的具體用法?Java WatchService.take怎麽用?Java WatchService.take使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.nio.file.WatchService的用法示例。


在下文中一共展示了WatchService.take方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: watch

import java.nio.file.WatchService; //導入方法依賴的package包/類
private void watch() {
    try {
        WatchService watchService = directoryPath.getFileSystem().newWatchService();
        directoryPath.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
                StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE);
        while (true) {
            WatchKey watchKey = watchService.take();
            for (final WatchEvent<?> event : watchKey.pollEvents()) {
                takeActionOnChangeEvent(event);
            }
        }

    } catch (InterruptedException interruptedException) {
        System.out.println("Thread got interrupted:" + interruptedException);
    } catch (Exception exception) {
        exception.printStackTrace();
    }

}
 
開發者ID:patexoid,項目名稱:ZombieLib2,代碼行數:20,代碼來源:DirWatcherService.java

示例2: run

import java.nio.file.WatchService; //導入方法依賴的package包/類
@Override
public void run() {
    WatchService watchService = WatchServiceUtil.watchModify(pluginDir);
    WatchKey key;
    while (watchService != null){
        try {
            key = watchService.take();
            for (WatchEvent<?> watchEvent : key.pollEvents()) {
                if(watchEvent.kind() == ENTRY_MODIFY){
                    String fileName = watchEvent.context() == null ? "" : watchEvent.context().toString();
                    Plugin plugin = PluginLibraryHelper.getPluginByConfigFileName(fileName);
                    if(plugin != null){
                        plugin.init(PluginLibraryHelper.getPluginConfig(plugin));
                        log.info("已完成插件{}的配置重新加載",plugin.pluginName());
                    }
                }
            }
            key.reset();
        } catch (Exception e) {
            log.error("插件配置文件監聽異常",e);
            break;
        }
    }
}
 
開發者ID:DevopsJK,項目名稱:SuitAgent,代碼行數:25,代碼來源:PluginPropertiesWatcher.java

示例3: setWatcherOnThemeFile

import java.nio.file.WatchService; //導入方法依賴的package包/類
private static void setWatcherOnThemeFile() {
    try {
        WatchService watchService = FileSystems.getDefault().newWatchService();
        WatchKey watchKey = path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
        while (true) {
            final WatchKey wk = watchService.take();
            for (WatchEvent<?> event : wk.pollEvents()) {
                //we only register "ENTRY_MODIFY" so the context is always a Path.
                final Path changed = (Path) event.context();
                System.out.println(changed);
                if (changed.endsWith("Theme.css")) {
                    System.out.println("Theme.css has changed...reloading stylesheet.");
                    scene.getStylesheets().clear();
                    scene.getStylesheets().add("resources/Theme.css");
                }
            }
            boolean valid = wk.reset();
            if (!valid)
                System.out.println("Watch Key has been reset...");
        }
    } catch (Exception e) { /*Thrown to void*/ }
}
 
開發者ID:maximstewart,項目名稱:UDE,代碼行數:23,代碼來源:UFM.java

示例4: simpleTest

import java.nio.file.WatchService; //導入方法依賴的package包/類
public void simpleTest(Path path) throws Exception
{
	WatchService watchService=FileSystems.getDefault().newWatchService();  
	path.register(watchService,   
            StandardWatchEventKinds.ENTRY_CREATE,  
            StandardWatchEventKinds.ENTRY_DELETE,  
            StandardWatchEventKinds.ENTRY_MODIFY);  
    while(true)  
    {  
        WatchKey watchKey=watchService.take();  
           List<WatchEvent<?>> watchEvents = watchKey.pollEvents();  
           for(WatchEvent<?> event : watchEvents){  
               //TODO 根據事件類型采取不同的操作。。。。。。。  
               System.out.println("["+event.context()+"]文件發生了["+event.kind()+"]事件");    
           }  
           watchKey.reset(); 
    } 
}
 
開發者ID:juebanlin,項目名稱:util4j,代碼行數:19,代碼來源:FileMonitorJdkImpl.java

示例5: poll

import java.nio.file.WatchService; //導入方法依賴的package包/類
/**
 * Polls the given WatchService in a tight loop. This keeps the event
 * queue drained, it also hogs a CPU core which seems necessary to
 * tickle the original bug.
 */
static void poll(WatchService watcher) {
    try {
        for (;;) {
            WatchKey key = watcher.take();
            if (key != null) {
                key.pollEvents();
                key.reset();
            }
        }
    } catch (ClosedWatchServiceException expected) {
        // nothing to do
    } catch (Exception e) {
        e.printStackTrace();
        failed = true;
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:22,代碼來源:LotsOfCancels.java

示例6: poll

import java.nio.file.WatchService; //導入方法依賴的package包/類
/**
 * Polls the given WatchService in a tight loop. This keeps the event
 * queue drained, it also hogs a CPU core which seems necessary to
 * tickle the original bug.
 */
static void poll(int id, WatchService watcher) {
    System.out.printf("begin poll %d%n", id);
    try {
        for (;;) {
            WatchKey key = watcher.take();
            if (key != null) {
                key.pollEvents();
                key.reset();
            }
        }
    } catch (ClosedWatchServiceException expected) {
        // nothing to do but print
        System.out.printf("poll %d expected exception %s%n", id, expected);
    } catch (Exception e) {
        e.printStackTrace();
        failed = true;
    }
    System.out.printf("end poll %d%n", id);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:25,代碼來源:LotsOfCancels.java

示例7: main

import java.nio.file.WatchService; //導入方法依賴的package包/類
public static void main(String[] args) throws Throwable {
    String tempDirPath = "/tmp";
    System.out.println("Starting watcher for " + tempDirPath);
    Path p = Paths.get(tempDirPath);
    WatchService watcher = 
        FileSystems.getDefault().newWatchService();
    Kind<?>[] watchKinds = { ENTRY_CREATE, ENTRY_MODIFY };
    p.register(watcher, watchKinds);
    mainRunner = Thread.currentThread();
    new Thread(new DemoService()).start();
    while (!done) {
        WatchKey key = watcher.take();
        for (WatchEvent<?> e : key.pollEvents()) {
            System.out.println(
                "Saw event " + e.kind() + " on " + 
                e.context());
            if (e.context().toString().equals("MyFileSema.for")) {
                System.out.println("Semaphore found, shutting down watcher");
                done = true;
            }
        }
        if (!key.reset()) {
            System.err.println("Key failed to reset!");
        }
    }
}
 
開發者ID:shashanksingh28,項目名稱:code-similarity,代碼行數:27,代碼來源:FileWatchServiceDemo.java

示例8: main

import java.nio.file.WatchService; //導入方法依賴的package包/類
public static void main(String[] args) throws IOException, InterruptedException {
	Path tmpDir = Paths.get("tmp");
	WatchService watchService = FileSystems.getDefault().newWatchService();
	Path monitoredFolder = tmpDir;
	monitoredFolder.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE);
	
	tmpDir.toFile().mkdirs();
	new FileChanger(tmpDir).start();
	while (true) {
		System.out.println("Waiting for event");
		WatchKey watchKey = watchService.take();
		for (WatchEvent<?> event : watchKey.pollEvents()) {
			System.out.println("Detected event " + event.kind().name() + " on file " + event.context().toString());
		}
		watchKey.reset();
	}
}
 
開發者ID:victorrentea,項目名稱:training,代碼行數:18,代碼來源:FileWatchService.java

示例9: main

import java.nio.file.WatchService; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception  
{  
      
    WatchService watchService=FileSystems.getDefault().newWatchService();  
    Paths.get("C:/").register(watchService,   
            StandardWatchEventKinds.ENTRY_CREATE,  
            StandardWatchEventKinds.ENTRY_DELETE,  
            StandardWatchEventKinds.ENTRY_MODIFY);  
    while(true)  
    {  
        WatchKey key=watchService.take();  
        for(WatchEvent<?> event:key.pollEvents())  
        {  
            System.out.println(event.context()+"發生了"+event.kind()+"事件");  
        }  
        if(!key.reset())  
        {  
            break;  
        }  
    }
}
 
開發者ID:East196,項目名稱:maker,代碼行數:22,代碼來源:WatchServiceTest.java

示例10: watch

import java.nio.file.WatchService; //導入方法依賴的package包/類
private static void watch() throws Exception
{
	WatchService watchService=FileSystems.getDefault().newWatchService();
	Paths.get("C:/").register(watchService, 
			StandardWatchEventKinds.ENTRY_CREATE,
			StandardWatchEventKinds.ENTRY_DELETE,
			StandardWatchEventKinds.ENTRY_MODIFY,StandardWatchEventKinds.OVERFLOW);
	
	while(true)
	{
		WatchKey key=watchService.take();
		//watchService.poll(10000, TimeUnit.valueOf("2014-8-26"));
		for(WatchEvent<?> event:key.pollEvents())
		{
			System.out.println(event.context()+"發生了"+event.kind()+"事件"+event.count());
		}
		if(!key.reset())
		{
			break;
		}
	}
}
 
開發者ID:yunshouhu,項目名稱:LogAnalyzer,代碼行數:23,代碼來源:Test.java

示例11: run

import java.nio.file.WatchService; //導入方法依賴的package包/類
@Override
public void run() {
    try {
        WatchService watchService = FileSystems.getDefault().newWatchService();
        propsFileFolder.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
        while (true) {
            WatchKey wk = watchService.take();
            for (WatchEvent<?> event : wk.pollEvents()) {
                Path changed = (Path) event.context();
                Path changedFile = propsFileFolder.resolve(changed.toString());
                if (changed.getFileName().toString().endsWith(fileName) && Files.exists(changedFile)) {
                    log.info("File '{}' changed. Updating values.", changedFile);
                    limits.tokenBody = FileLoaderUtil.readFileAsString(fileName);
                }
            }
            // reset the key
            boolean valid = wk.reset();
            if (!valid) {
                log.info("Key has been not unregistered.");
            }
        }
    } catch (IOException | InterruptedException e) {
        log.warn("Error monitoring '{}' file. Reloadable properties are not enabled.",
                SERVER_PROPERTIES_FILENAME, e);
    }
}
 
開發者ID:blynkkk,項目名稱:blynk-server,代碼行數:27,代碼來源:FileChangeWatcherWorker.java

示例12: main

import java.nio.file.WatchService; //導入方法依賴的package包/類
public static void main(String[] args)throws Exception {  
     
    //獲取當前文件係統的WatchService監控對象  
    WatchService watchService=FileSystems.getDefault().newWatchService();  
    //監聽的事件類型,有創建,刪除,以及修改  
    //Path file = FileSystems.getDefault().getPath(pathname);
    Paths.get("E:\\女神密電").register(watchService, StandardWatchEventKinds.ENTRY_CREATE,StandardWatchEventKinds.ENTRY_DELETE,StandardWatchEventKinds.ENTRY_MODIFY,StandardWatchEventKinds.OVERFLOW);  
  
  while(true){  
      //獲取下一個文件變化事件  
      WatchKey key=watchService.take();  
      for(WatchEvent<?> event:key.pollEvents()){  
            
          System.out.println(event.kind().name()+"文件發生了"+event.context().toString()+"事件"+"此事件發生的次數: "+event.count());  
      }  
      //重設WatchKey  
      boolean valid=key.reset();  
      //監聽失敗,退出監聽  
      if(!valid){  
          break;  
      }  
  }  
}
 
開發者ID:desperado1992,項目名稱:distributeTemplate,代碼行數:24,代碼來源:FileMonitorTest.java

示例13: main

import java.nio.file.WatchService; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
	Path path = Paths.get("/home/proberts/Desktop");
	WatchService watchService = path.getFileSystem().newWatchService();
	
	path.register(watchService,
			StandardWatchEventKinds.ENTRY_CREATE,
               StandardWatchEventKinds.ENTRY_MODIFY,
               StandardWatchEventKinds.ENTRY_DELETE);
	
	while(true) {
		WatchKey watchKey = watchService.take();
		
		for (WatchEvent<?> event : watchKey.pollEvents()) {
               printEvent(event);
           }
		
		if(!watchKey.reset()) {
			watchKey.cancel();
			watchService.close();
		}
	}
}
 
開發者ID:psxpaul,項目名稱:EclipseJava7Refresher,代碼行數:23,代碼來源:Java7RefreshProviderPlugin.java

示例14: initWatcher

import java.nio.file.WatchService; //導入方法依賴的package包/類
private void initWatcher() throws IOException, InterruptedException {
	WatchService watcher = path.getFileSystem().newWatchService();
	path.register(watcher,
			StandardWatchEventKinds.ENTRY_CREATE,
			StandardWatchEventKinds.ENTRY_MODIFY,
			StandardWatchEventKinds.ENTRY_DELETE);

	LOG.info("Now watching template folder: " + path.toFile().getAbsolutePath());

	while (true) {
		WatchKey key = watcher.take();
		List<WatchEvent<?>> events = key.pollEvents();
		if (!events.isEmpty()) {
			updateTemplates();
		}
		key.reset();
	}
}
 
開發者ID:devhub-tud,項目名稱:devhub-prototype,代碼行數:19,代碼來源:TemplateEngine.java

示例15: main

import java.nio.file.WatchService; //導入方法依賴的package包/類
public static void main(String[] args) throws InterruptedException {
	File file = new File(".");

	Path path = file.toPath();
	final WatchService watcher;
	try {
		watcher = FileSystems.getDefault().newWatchService();
		path.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);
	} catch (IOException e) {
		throw new RuntimeException(e);
	}

	WatchKey watchKey = watcher.take();
	while(true) {
		for (WatchEvent<?> event : watchKey.pollEvents()) {
			System.out.println(event + ", context:" + event.context() + ", kind:" + event.kind() + ", count:" + event.count());
		}
		watchKey.reset();
	}
}
 
開發者ID:sitoolkit,項目名稱:sit-ad,代碼行數:21,代碼來源:WatchServiceTrial.java


注:本文中的java.nio.file.WatchService.take方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。