public abstract class FileWatcher extends Object
class FooFileWatcher extends FileWatcher {
public FooFileWatcher() {
super(true); // true: keep reading.
}
public void onChange(File file) {
System.out.println(file.getAbsolutePath());
}
}
// create new instance
FooFileWatcher watcher = new FooFileWatcher();
// set inteval
watcher.setDelay(10 * 1000);
watcher.add(new File("/tmp/foo.txt"));
watcher.start();
watcher.add(new File("/tmp/foo.txt"));
wait(100*1000);
watcher.exit();
Thanks to contributions by Mathias Bogaert.
License was changed from apache 1.1 to Mozilla.
MMBase javadoc
This code was originally borrowed from the log4j project (as can still be seen from the authors),
it was however quite heavily adapted. You are probably better of using a ResourceWatcher
(since MMBase 1.8), because that does not watch only files. Its implementation does of course use
FileWatcher, for the 'file' part of the watching.| Modifier and Type | Class and Description |
|---|---|
(package private) static class |
FileWatcher.FileWatcherRunner
The one thread to handle all FileWatchers.
|
| Modifier and Type | Field and Description |
|---|---|
static long |
DEFAULT_DELAY
The default delay between every file modification check, set to 60
seconds.
|
(package private) static FileWatcher.FileWatcherRunner |
fileWatchers |
(package private) static ScheduledFuture<?> |
future |
static long |
THREAD_DELAY
The one thread doing al the work also needs a delay.
|
| Modifier | Constructor and Description |
|---|---|
protected |
FileWatcher() |
protected |
FileWatcher(boolean c) |
| Modifier and Type | Method and Description |
|---|---|
void |
add(File file)
Add's a file to be checked...
|
void |
clear()
Removes all files, this watcher will end up watching nothing.
|
boolean |
contains(File file)
Wether the file is being watched or not.
|
void |
exit()
Stops watching.
|
long |
getDelay() |
Set<File> |
getFiles()
Returns a (modifiable) Set of all files (File object) of this FileWatcher.
|
static Set<FileWatcher> |
getFileWatchers() |
Date |
getLastCheck() |
boolean |
isContinueAfterChange() |
boolean |
isRunning() |
static void |
main(String[] args) |
abstract void |
onChange(File file)
Put here the stuff that has to be executed, when a file has been changed.
|
void |
remove(File file)
Remove file from the watch-list
|
(package private) static void |
scheduleFileWatcherRunner() |
void |
setDelay(long delay)
Set the delay to observe between each check of the file changes.
|
static void |
shutdown() |
void |
start() |
String |
toString()
Shows the 'contents' of the filewatcher.
|
public static final long DEFAULT_DELAY
public static long THREAD_DELAY
static ScheduledFuture<?> future
static FileWatcher.FileWatcherRunner fileWatchers
static void scheduleFileWatcherRunner()
public static void shutdown()
public void start()
public abstract void onChange(File file)
file - The file that was changed..public void setDelay(long delay)
delay - The delay in millisecondspublic long getDelay()
public boolean isRunning()
public boolean isContinueAfterChange()
public Date getLastCheck()
public void add(File file)
file - The file which has to be monitored..RuntimeException - If file is nullpublic boolean contains(File file)
file - the file to be checked.public void remove(File file)
public Set<File> getFiles()
LinkedHashSet).public void clear()
public void exit()
public String toString()
public static Set<FileWatcher> getFileWatchers()
public static void main(String[] args)
MMBase 1.9-SNAPSHOT - ${javadoctimestamp}