|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.commons.io.monitor.FileAlterationObserver
public class FileAlterationObserver
FileAlterationObserver represents the state of files below a root directory, checking the filesystem and notifying listeners of create, change or delete events.
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(new File("."), "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:
// intialize observer.init(); ... // invoke as required observer.checkAndNotify(); ... observer.checkAndNotify(); ... // finished observer.finish();Alternatively, register the oberver(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();
FileFilter
s 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.
FileAlterationListener
,
FileAlterationMonitor
,
Serialized FormConstructor Summary | |
---|---|
|
FileAlterationObserver(File directory)
Construct an observer for the specified directory. |
protected |
FileAlterationObserver(FileEntry rootEntry,
FileFilter fileFilter,
IOCase caseSensitivity)
Construct an observer for the specified directory, file filter and file comparator. |
|
FileAlterationObserver(File directory,
FileFilter fileFilter)
Construct an observer for the specified directory and file filter. |
|
FileAlterationObserver(File directory,
FileFilter fileFilter,
IOCase caseSensitivity)
Construct an observer for the specified directory, file filter and file comparator. |
|
FileAlterationObserver(String directoryName)
Construct an observer for the specified directory. |
|
FileAlterationObserver(String directoryName,
FileFilter fileFilter)
Construct an observer for the specified directory and file filter. |
|
FileAlterationObserver(String directoryName,
FileFilter fileFilter,
IOCase caseSensitivity)
Construct an observer for the specified directory, file filter and file comparator. |
Method Summary | |
---|---|
void |
addListener(FileAlterationListener listener)
Add a file system listener. |
void |
checkAndNotify()
Check whether the file and its chlidren have been created, modified or deleted. |
void |
destroy()
Final processing. |
File |
getDirectory()
Return the directory being observed. |
FileFilter |
getFileFilter()
Return the fileFilter. |
Iterable<FileAlterationListener> |
getListeners()
Returns the set of registered file system listeners. |
void |
initialize()
Initialize the observer. |
void |
removeListener(FileAlterationListener listener)
Remove a file system listener. |
String |
toString()
Provide a String representation of this observer. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public FileAlterationObserver(String directoryName)
directoryName
- the name of the directory to observepublic FileAlterationObserver(String directoryName, FileFilter fileFilter)
directoryName
- the name of the directory to observefileFilter
- The file filter or null if nonepublic FileAlterationObserver(String directoryName, FileFilter fileFilter, IOCase caseSensitivity)
directoryName
- the name of the directory to observefileFilter
- The file filter or null if nonecaseSensitivity
- what case sensitivity to use comparing file names, null means system sensitivepublic FileAlterationObserver(File directory)
directory
- the directory to observepublic FileAlterationObserver(File directory, FileFilter fileFilter)
directory
- the directory to observefileFilter
- The file filter or null if nonepublic FileAlterationObserver(File directory, FileFilter fileFilter, IOCase caseSensitivity)
directory
- the directory to observefileFilter
- The file filter or null if nonecaseSensitivity
- what case sensitivity to use comparing file names, null means system sensitiveprotected FileAlterationObserver(FileEntry rootEntry, FileFilter fileFilter, IOCase caseSensitivity)
rootEntry
- the root directory to observefileFilter
- The file filter or null if nonecaseSensitivity
- what case sensitivity to use comparing file names, null means system sensitiveMethod Detail |
---|
public File getDirectory()
public FileFilter getFileFilter()
public void addListener(FileAlterationListener listener)
listener
- The file system listenerpublic void removeListener(FileAlterationListener listener)
listener
- The file system listenerpublic Iterable<FileAlterationListener> getListeners()
public void initialize() throws Exception
Exception
- if an error occurspublic void destroy() throws Exception
Exception
- if an error occurspublic void checkAndNotify()
public String toString()
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |