FileWatcher
classin packageklyn.io
public class FileWatcher extends Thread implements AutoClosable:
└ Thread
└ FileWatcher
All Implemented Interfaces: AutoClosable

Portable file watcher based on periodic filesystem metadata polling.

The watcher observes a single path and emits typed events when existence, modification timestamp or size changes. It is implemented in Klyn and uses the existing Path metadata API, so it works consistently across platforms.

import klyn.io

watcher = FileWatcher("app.log", 250)
watcher.updated += lambda(e: FileWatcherEvent): print("changed: " + e.path)
Fields
Modifier and Type Member Description
public event changed
changed as FileWatcherEvent
Emitted for every detected file state change.
public event created
created as FileWatcherEvent
Emitted when the watched file appears.
public event deleted
deleted as FileWatcherEvent
Emitted when the watched file disappears.
public event modified
modified as FileWatcherEvent
Emitted when an existing watched file changes size or timestamp.
public event updated
updated as FileWatcherEvent
Emitted when an existing watched file changes size or timestamp.
Properties
Modifier and Type Member Description
public readonly property isWatching
isWatching as Boolean:
Returns true while the background watcher loop is running.
public readonly property path
path as Path:
Watched path.
public property pollIntervalMs
pollIntervalMs as Int:
Polling interval in milliseconds.
Properties inherited from Thread: activeCount, currentThread, id, isAlive, isInterrupted, name, priority, state
Properties inherited from Object: type
Constructors
Modifier and Type Member Description
public FileWatcher
FileWatcher(path as Path, pollIntervalMs as Int = 50):
Creates a watcher for a Path instance.
public FileWatcher
FileWatcher(path as String, pollIntervalMs as Int = 50):
Creates a watcher for a path string.
Methods
Modifier and Type Member Description
public close
close() as Void:
AutoClosable support for try-with-resources.
public poll
poll() as Boolean:
Polls once and emits events if the file state changed.
public resetBaseline
resetBaseline() as Void:
Resets the baseline without emitting events.
public override run
run() as Void:
Background watcher loop.
public override start
start() as Void:
Starts background polling.
public override stop
stop() as Void:
Stops background polling.
Methods inherited from Thread: current, interrupt, join, join, resume, sleep, start, stop, suspend, toString, withTarget, yield
Methods inherited from AutoClosable: close
Methods inherited from Object: deepCopy, fromJson, toDict, toJson, toString, toXml
Methods inherited from Runnable: run