public class FileWatcher extends Thread
This thread monitors one or more files, checking them at preset intervals. A listener is notified of the findings.
The check is performed every n seconds,
where the interval n can be configured
(see setInterval(int) and getInterval()).
Initially this thread will be a daemon thread. This can be changed by
calling Thread.setDaemon(boolean).
| Modifier and Type | Class and Description |
|---|---|
static interface |
FileWatcher.Listener
Interface for file watcher listeners.
|
Thread.State, Thread.UncaughtExceptionHandler| Modifier and Type | Field and Description |
|---|---|
protected String |
_filePaths
The string representation of the files to watch.
|
protected long |
_lastModified
Timestamp of the last modification of the file.
|
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY| Constructor and Description |
|---|
FileWatcher(String[] files,
int interval,
FileWatcher.Listener listener)
Creates a new
FileWatcher for the specified set of files,
with the specified interval. |
FileWatcher(String file,
FileWatcher.Listener listener)
Creates a new
FileWatcher for the specified file. |
FileWatcher(String file,
int interval,
FileWatcher.Listener listener)
Creates a new
FileWatcher for the specified file, with the
specified interval. |
| Modifier and Type | Method and Description |
|---|---|
void |
check()
Checks if the file changed.
|
void |
end()
Stops this thread.
|
protected void |
firstCheck()
Performs the first check on the file to determine the date the file was
last modified.
|
int |
getInterval()
Returns the current interval.
|
protected long |
getLastModified()
Gets the time at which the last file was modified.
|
void |
run()
Runs this thread.
|
void |
setInterval(int newInterval)
Changes the file check interval.
|
protected void |
storeFiles(String[] files)
Stores the files in a class variable.
|
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yieldprotected String _filePaths
null.protected long _lastModified
-1L indicates that the file could not be found the last
time this was checked.
Initially this field is -1L.
public FileWatcher(String file, FileWatcher.Listener listener) throws IllegalArgumentException
FileWatcher for the specified file.
The interval must be set before the thread can be started.
file - the name of the file to watch, cannot be null.listener - the object to notify on events, cannot be null.IllegalArgumentException - if file == null || listener == nullpublic FileWatcher(String file, int interval, FileWatcher.Listener listener) throws IllegalArgumentException
FileWatcher for the specified file, with the
specified interval.file - the name of the file to watch, cannot be null.interval - the interval in seconds, must be greater than or equal to 0.
if the interval is 0 the interval must be set before the thread can
be started.listener - the object to notify on events, cannot be null.IllegalArgumentException - if file == null || listener == null || interval < 0public FileWatcher(String[] files, int interval, FileWatcher.Listener listener) throws IllegalArgumentException
FileWatcher for the specified set of files,
with the specified interval.files - the name of the files to watch, cannot be null.
It should also have at least one file and none of the file should be null.interval - the interval in seconds, must be greater than or equal to 0.
if the interval is 0 the interval must be set before the thread can
be started.listener - the object to notify on events, cannot be null.IllegalArgumentException - if files == null || listener == null || interval < 0 || files.length < 1
or if one of the file is null.protected void storeFiles(String[] files)
files - the String files to check, cannot be null.protected void firstCheck()
SecurityException, then this
exception is logged and ignored.public void run()
throws IllegalStateException
Thread.start() instead. That method will call this method.run in interface Runnablerun in class ThreadIllegalStateException - if Thread.currentThread() != this, if the thread
is already running or should stop, or if the interval was not set
yet.public int getInterval()
public void setInterval(int newInterval)
throws IllegalArgumentException
newInterval - the new interval in seconds, must be greater than or equal to 1.IllegalArgumentException - if interval < 1public void end()
throws IllegalStateException
IllegalStateException - if the thread is currently not running or already stopping.public void check()
SecurityException to be thrown, then
FileWatcher.Listener.securityException(SecurityException) is called
and the method returns;
FileWatcher.Listener.fileNotFound() is called and the method returns;
FileWatcher.Listener.fileFound() is called and the method
returns;
FileWatcher.Listener.fileModified() is called and the method returns;
FileWatcher.Listener.fileNotModified() is called and the method
returns.
protected long getLastModified()
throws SecurityException
SecurityException - if one of the file could not be read because of a security issue.See http://www.xins.org/.