Package

Source Code of WatchMe

import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;

import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;

public class WatchMe {
    public static void main(String[] args) {
        Path currentDirectory = Paths.get(System.getProperty("java.io.tmpdir"));

        try (WatchService watchService = FileSystems.getDefault().newWatchService()) {
            WatchKey watchKey = currentDirectory.register(watchService, ENTRY_CREATE, ENTRY_DELETE,
                    ENTRY_MODIFY);
           
            do {
                watchKey = watchService.take();

                for (WatchEvent<?> event : watchKey.pollEvents()) {
                    System.out.println(String.format("%s event on %s",
                            event.kind(), event.context()));
                }
            } while (watchKey.reset());
        } catch (Exception e) {
        }
    }
}
TOP

Related Classes of WatchMe

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.