Skip to content

io microsphere io StandardFileWatchService

github-actions[bot] edited this page Mar 21, 2026 · 1 revision

StandardFileWatchService

Type: Class | Module: microsphere-java-core | Package: io.microsphere.io | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/io/StandardFileWatchService.java

Overview

/** Standard implementation of the FileWatchService interface using JDK 7's WatchService API. This class monitors files or directories for creation, modification, and deletion events.

Key Features

- Supports watching individual files or entire directories.
- Allows filtering based on event types: CREATED, MODIFIED, DELETED.
- Uses a thread-safe design to handle concurrent listeners and events.
- Provides auto-closeable behavior via the `#close()` method.

Example Usage

`// Example 1: Watching a single file for modification events
FileWatchService watchService = new StandardFileWatchService();
File fileToWatch = new File("/path/to/file.txt");
FileChangedListener listener = event -> System.out.println("File changed: " + event.getFile());

watchService.watch(fileToWatch, listener, FileChangedEvent.Kind.MODIFIED);

// Example 2: Watching a directory for all types of events with multiple listeners
File dirToWatch = new File("/path/to/directory");
List listeners = Arrays.asList(
    event -> System.out.println("Listener 1 triggered: " + event),
    event -> System.out.println("Listener 2 triggered: " + event)
);

watchService.watch(dirToWatch, listeners); // All kinds by default
`

Make sure to call #close() when you're done using the service to release resources.

Declaration

public class StandardFileWatchService implements FileWatchService, AutoCloseable

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.1.10-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

// Example 1: Watching a single file for modification events
FileWatchService watchService = new StandardFileWatchService();
File fileToWatch = new File("/path/to/file.txt");
FileChangedListener listener = event -> System.out.println("File changed: " + event.getFile());

watchService.watch(fileToWatch, listener, FileChangedEvent.Kind.MODIFIED);

// Example 2: Watching a directory for all types of events with multiple listeners
File dirToWatch = new File("/path/to/directory");
List<FileChangedListener> listeners = Arrays.asList(
    event -> System.out.println("Listener 1 triggered: " + event),
    event -> System.out.println("Listener 2 triggered: " + event)
);

watchService.watch(dirToWatch, listeners); // All kinds by default

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.io.StandardFileWatchService;

API Reference

Public Methods

Method Description
start
stop
isStarted
close
watch

See Also

  • WatchService

This documentation was auto-generated from the source code of microsphere-java.

Home

java-annotations

java-core

jdk-tools

lang-model

annotation-processor

java-test

Clone this wiki locally