File System Watch Service Event Option - Java File Path IO

Java examples for File Path IO:File System

Introduction

The supported types of events are defined in the StandardWatchEventKinds.

  • StandardWatchEventKinds.ENTRY_CREATE: A directory entry is created or a file is renamed or moved into this directory.
  • StandardWatchEventKinds.ENTRY_DELETE: A directory entry is deleted or a file is renamed or moved out of this directory.
  • StandardWatchEventKinds.ENTRY_MODIFY: A directory entry is modified.
  • StandardWatchEventKinds.OVERFLOW: events might have been lost or discarded.

The following code snippet registers the Path with the watch service with the monitored events: create, delete, and modify.

Demo Code

import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchService;

public class Main {
  public static void main(String[] args) throws Exception {
    final Path path = Paths.get("C:/folder1");
    WatchService watchService = FileSystems.getDefault().newWatchService();

    path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
        StandardWatchEventKinds.ENTRY_MODIFY,
        StandardWatchEventKinds.ENTRY_DELETE);

    watchService.close();/*from w w w .ja va2s  .com*/

  }
}

Related Tutorials