public class FileAlterationObserver
extends java.lang.Object
implements java.io.Serializable
To use this implementation:
FileAlterationListener implementation(s) that process
the file/directory create, change and delete eventsFileAlterationObserver for
the appropriate directory.FileAlterationMonitor or
run manually.FileAlterationObserver for the directory and register the listeners:
File directory = new File(FileUtils.current(), "src");
FileAlterationObserver observer = new FileAlterationObserver(directory);
observer.addListener(...);
observer.addListener(...);
To manually observe a directory, initialize the observer and invoked the
checkAndNotify() method as required:
// initialize
observer.init();
...
// invoke as required
observer.checkAndNotify();
...
observer.checkAndNotify();
...
// finished
observer.finish();
Alternatively, register the observer(s) with a FileAlterationMonitor,
which creates a new thread, invoking the observer at the specified interval:
long interval = ...
FileAlterationMonitor monitor = new FileAlterationMonitor(interval);
monitor.addObserver(observer);
monitor.start();
...
monitor.stop();
FileFilters to observe only the files and/or directories
that are of interest. This makes it more efficient and reduces the
noise from unwanted file system events.
Commons IO has a good range of useful, ready-made File Filter implementations for this purpose.
For example, to only observe 1) visible directories and 2) files with a ".java" suffix
in a root directory called "src" you could set up a FileAlterationObserver in the following
way:
// Create a FileFilter
IOFileFilter directories = FileFilterUtils.and(
FileFilterUtils.directoryFileFilter(),
HiddenFileFilter.VISIBLE);
IOFileFilter files = FileFilterUtils.and(
FileFilterUtils.fileFileFilter(),
FileFilterUtils.suffixFileFilter(".java"));
IOFileFilter filter = FileFilterUtils.or(directories, files);
// Create the File system observer and register File Listeners
FileAlterationObserver observer = new FileAlterationObserver(new File("src"), filter);
observer.addListener(...);
observer.addListener(...);
FileEntry represents the state of a file or directory, capturing
File attributes at a point in time. Custom implementations of
FileEntry can be used to capture additional properties that the
basic implementation does not support. The FileEntry.refresh(File)
method is used to determine if a file or directory has changed since the last
check and stores the current state of the File's properties.
Serialization is deprecated and will be removed in 3.0.
FileAlterationListener,
FileAlterationMonitor,
Serialized Form| Constructor and Description |
|---|
FileAlterationObserver(java.io.File directory)
Constructs an observer for the specified directory.
|
FileAlterationObserver(java.io.File directory,
java.io.FileFilter fileFilter)
Constructs an observer for the specified directory and file filter.
|
FileAlterationObserver(java.io.File directory,
java.io.FileFilter fileFilter,
IOCase ioCase)
Constructs an observer for the specified directory, file filter and
file comparator.
|
FileAlterationObserver(java.lang.String directoryName)
Constructs an observer for the specified directory.
|
FileAlterationObserver(java.lang.String directoryName,
java.io.FileFilter fileFilter)
Constructs an observer for the specified directory and file filter.
|
FileAlterationObserver(java.lang.String directoryName,
java.io.FileFilter fileFilter,
IOCase ioCase)
Constructs an observer for the specified directory, file filter and file comparator.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addListener(FileAlterationListener listener)
Adds a file system listener.
|
void |
checkAndNotify()
Checks whether the file and its children have been created, modified or deleted.
|
void |
destroy()
Final processing.
|
java.io.File |
getDirectory()
Returns the directory being observed.
|
java.io.FileFilter |
getFileFilter()
Returns the fileFilter.
|
java.lang.Iterable<FileAlterationListener> |
getListeners()
Returns the set of registered file system listeners.
|
void |
initialize()
Initializes the observer.
|
void |
removeListener(FileAlterationListener listener)
Removes a file system listener.
|
java.lang.String |
toString()
Returns a String representation of this observer.
|
public FileAlterationObserver(java.io.File directory)
directory - the directory to observepublic FileAlterationObserver(java.io.File directory,
java.io.FileFilter fileFilter)
directory - the directory to observefileFilter - The file filter or null if nonepublic FileAlterationObserver(java.io.File directory,
java.io.FileFilter fileFilter,
IOCase ioCase)
directory - the directory to observefileFilter - The file filter or null if noneioCase - what case sensitivity to use comparing file names, null means system sensitivepublic FileAlterationObserver(java.lang.String directoryName)
directoryName - the name of the directory to observepublic FileAlterationObserver(java.lang.String directoryName,
java.io.FileFilter fileFilter)
directoryName - the name of the directory to observefileFilter - The file filter or null if nonepublic FileAlterationObserver(java.lang.String directoryName,
java.io.FileFilter fileFilter,
IOCase ioCase)
directoryName - the name of the directory to observefileFilter - The file filter or null if noneioCase - what case sensitivity to use comparing file names, null means system sensitivepublic void addListener(FileAlterationListener listener)
listener - The file system listenerpublic void checkAndNotify()
public void destroy()
throws java.lang.Exception
java.lang.Exception - if an error occurspublic java.io.File getDirectory()
public java.io.FileFilter getFileFilter()
public java.lang.Iterable<FileAlterationListener> getListeners()
public void initialize()
throws java.lang.Exception
java.lang.Exception - if an error occurspublic void removeListener(FileAlterationListener listener)
listener - The file system listenerpublic java.lang.String toString()
toString in class java.lang.ObjectCopyright © 2010 - 2023 Adobe. All Rights Reserved