Skip to content

io microsphere io FileWatchService

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

FileWatchService

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

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

Overview

A service that watches files or directories for changes and notifies registered listeners.

This interface provides methods to register listeners for specific files or directories, specifying the types of events the listener is interested in. When a watched file or directory experiences an event (e.g., creation, modification, deletion), the associated listeners are notified.

Example Usage

`// Example 1: Watching a single file with a single listener and specific event kinds
FileWatchService watchService = ...; // implementation instance
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 with multiple listeners and all event kinds
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
`

Declaration

public interface FileWatchService

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 with a single listener and specific event kinds
FileWatchService watchService = ...; // implementation instance
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 with multiple listeners and all event kinds
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.FileWatchService;

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