本文整理汇总了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();
}
}
示例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;
}
}
}
示例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*/ }
}
示例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();
}
}
示例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;
}
}
示例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);
}
示例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!");
}
}
}
示例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();
}
}
示例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;
}
}
}
示例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;
}
}
}
示例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);
}
}
示例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;
}
}
}
示例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();
}
}
}
示例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();
}
}
示例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();
}
}