Class FileAlterationObserver
- All Implemented Interfaces:
Serializable
To use this implementation:
- Create
FileAlterationListenerimplementation(s) that process the file/directory create, change and delete events - Register the listener(s) with a
FileAlterationObserverfor the appropriate directory. - Either register the observer(s) with a
FileAlterationMonitoror run manually.
Basic Usage
Create aFileAlterationObserver 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();
File Filters
This implementation can monitor portions of the file system by usingFileFilters 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
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.
Deprecating Serialization
Serialization is deprecated and will be removed in 3.0.
- Since:
- 2.0
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Comparator<File> Compares file names.private final FileFilterThe file filter or null if none.private final List<FileAlterationListener> List of listeners.private final FileEntryThe root directory to observe.private static final long -
Constructor Summary
ConstructorsModifierConstructorDescriptionFileAlterationObserver(File directory) Constructs an observer for the specified directory.FileAlterationObserver(File directory, FileFilter fileFilter) Constructs an observer for the specified directory and file filter.FileAlterationObserver(File directory, FileFilter fileFilter, IOCase ioCase) Constructs an observer for the specified directory, file filter and file comparator.FileAlterationObserver(String directoryName) Constructs an observer for the specified directory.FileAlterationObserver(String directoryName, FileFilter fileFilter) Constructs an observer for the specified directory and file filter.FileAlterationObserver(String directoryName, FileFilter fileFilter, IOCase ioCase) Constructs an observer for the specified directory, file filter and file comparator.privateFileAlterationObserver(FileEntry rootEntry, FileFilter fileFilter, Comparator<File> comparator) Constructs an observer for the specified directory, file filter and file comparator.protectedFileAlterationObserver(FileEntry rootEntry, FileFilter fileFilter, IOCase ioCase) Constructs an observer for the specified directory, file filter and file comparator. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(FileAlterationListener listener) Adds a file system listener.private voidcheckAndFire(FileEntry parentEntry, FileEntry[] previousEntries, File[] currentEntries) Compares two file lists for files which have been created, modified or deleted.voidChecks whether the file and its children have been created, modified or deleted.private FileEntrycreateFileEntry(FileEntry parent, File file) Creates a new file entry for the specified file.voiddestroy()Final processing.private voidfireOnChange(FileEntry entry, File file) Fires directory/file change events to the registered listeners.private voidfireOnCreate(FileEntry entry) Fires directory/file created events to the registered listeners.private voidfireOnDelete(FileEntry entry) Fires directory/file delete events to the registered listeners.Returns the directory being observed.Returns the fileFilter.Returns the set of registered file system listeners.voidInitializes the observer.private FileEntry[]listFileEntries(File file, FileEntry entry) Lists the file entries infile.private File[]Lists the contents of a directory.voidremoveListener(FileAlterationListener listener) Removes a file system listener.private File[]private static Comparator<File> toComparator(IOCase ioCase) toString()Returns a String representation of this observer.
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
listeners
List of listeners. -
rootEntry
The root directory to observe. -
fileFilter
The file filter or null if none. -
comparator
Compares file names.
-
-
Constructor Details
-
FileAlterationObserver
Constructs an observer for the specified directory.- Parameters:
directory- the directory to observe.
-
FileAlterationObserver
Constructs an observer for the specified directory and file filter.- Parameters:
directory- the directory to observe.fileFilter- The file filter or null if none.
-
FileAlterationObserver
Constructs an observer for the specified directory, file filter and file comparator.- Parameters:
directory- the directory to observe.fileFilter- The file filter or null if none.ioCase- what case sensitivity to use comparing file names, null means system sensitive.
-
FileAlterationObserver
private FileAlterationObserver(FileEntry rootEntry, FileFilter fileFilter, Comparator<File> comparator) Constructs an observer for the specified directory, file filter and file comparator.- Parameters:
rootEntry- the root directory to observe.fileFilter- The file filter or null if none.comparator- how to compare files.
-
FileAlterationObserver
Constructs an observer for the specified directory, file filter and file comparator.- Parameters:
rootEntry- the root directory to observe.fileFilter- The file filter or null if none.ioCase- what case sensitivity to use comparing file names, null means system sensitive.
-
FileAlterationObserver
Constructs an observer for the specified directory.- Parameters:
directoryName- the name of the directory to observe.
-
FileAlterationObserver
Constructs an observer for the specified directory and file filter.- Parameters:
directoryName- the name of the directory to observe.fileFilter- The file filter or null if none.
-
FileAlterationObserver
Constructs an observer for the specified directory, file filter and file comparator.- Parameters:
directoryName- the name of the directory to observe.fileFilter- The file filter or null if none.ioCase- what case sensitivity to use comparing file names, null means system sensitive.
-
-
Method Details
-
toComparator
-
addListener
Adds a file system listener.- Parameters:
listener- The file system listener.
-
checkAndFire
private void checkAndFire(FileEntry parentEntry, FileEntry[] previousEntries, File[] currentEntries) Compares two file lists for files which have been created, modified or deleted.- Parameters:
parentEntry- The parent entry.previousEntries- The original list of file entries.currentEntries- The current list of files entries.
-
checkAndNotify
public void checkAndNotify()Checks whether the file and its children have been created, modified or deleted. -
createFileEntry
Creates a new file entry for the specified file.- Parameters:
parent- The parent file entry.file- The file to wrap.- Returns:
- A new file entry.
-
destroy
Final processing.- Throws:
Exception- if an error occurs.
-
fireOnChange
Fires directory/file change events to the registered listeners.- Parameters:
entry- The previous file system entry.file- The current file.
-
fireOnCreate
Fires directory/file created events to the registered listeners.- Parameters:
entry- The file entry.
-
fireOnDelete
Fires directory/file delete events to the registered listeners.- Parameters:
entry- The file entry.
-
getDirectory
Returns the directory being observed.- Returns:
- the directory being observed.
-
getFileFilter
Returns the fileFilter.- Returns:
- the fileFilter.
- Since:
- 2.1
-
getListeners
Returns the set of registered file system listeners.- Returns:
- The file system listeners
-
initialize
Initializes the observer.- Throws:
Exception- if an error occurs.
-
listFileEntries
Lists the file entries infile.- Parameters:
file- The directory to list.entry- the parent entry.- Returns:
- The child file entries.
-
listFiles
Lists the contents of a directory.- Parameters:
directory- The directory to list.- Returns:
- the directory contents or a zero length array if the empty or the file is not a directory
-
removeListener
Removes a file system listener.- Parameters:
listener- The file system listener.
-
sort
-
toString
Returns a String representation of this observer.
-