Course contentsShow
← Java
Lesson 2097 of 2,87041. NIO.2 and the Path APIFree lesson

WatchEvent.Kind: Event Types to Monitor

Understanding ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY, and OVERFLOW event kinds.

WatchEvent.Kind: Event Types to Monitor

What you'll learn: How to specify which types of filesystem changes you want the WatchService to detect.

When you register a directory with the WatchService, you don't monitor everything—you choose exactly which types of events matter to you. This is done by specifying one or more WatchEvent.Kind constants that represent different filesystem operations.

The Four Standard Event Kinds

Java provides four predefined event types through the StandardWatchEventKinds class:

ENTRY_CREATE

Triggered when a new file or directory is created inside the watched directory. Think of it as a "birth announcement" for new filesystem entries.

ENTRY_DELETE

Fired when a file or directory is removed from the watched directory. This is your "deletion notification."

ENTRY_MODIFY

Activated when an existing file's content is changed. Important: this doesn't trigger for all attribute changes—primarily content modifications. The exact behavior can be platform-dependent.

OVERFLOW

A special event indicating that events may have been lost or discarded. This happens when the event queue fills up faster than your application can process events—like a buffer overflow warning. When you receive this, some filesystem changes went undetected.

Choosing Your Events

When registering a path, you pass the event kinds you care about:

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

This example only monitors file creation and modification, ignoring deletions entirely.

Think of these kinds like checkboxes on a notification preference form—you select what alerts you want to receive, and the system only bothers you with those specific events.

Key Takeaway: The four WatchEvent.Kind constants (ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY, and OVERFLOW) let you precisely specify which filesystem changes your WatchService monitors, allowing you to filter events at registration time rather than processing everything.