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:
- 
Nested Class SummaryNested Classes
- 
Constructor SummaryConstructorsModifierConstructorDescriptionFileAlterationObserver(File directory) Deprecated.FileAlterationObserver(File directory, FileFilter fileFilter) Deprecated.Usebuilder().FileAlterationObserver(File directory, FileFilter fileFilter, IOCase ioCase) Deprecated.Usebuilder().FileAlterationObserver(String directoryName) Deprecated.Usebuilder().FileAlterationObserver(String directoryName, FileFilter fileFilter) Deprecated.Usebuilder().FileAlterationObserver(String directoryName, FileFilter fileFilter, IOCase ioCase) Deprecated.Usebuilder().protectedFileAlterationObserver(FileEntry rootEntry, FileFilter fileFilter, IOCase ioCase) Constructs an observer for the specified directory, file filter and file comparator.
- 
Method SummaryModifier and TypeMethodDescriptionvoidaddListener(FileAlterationListener listener) Adds a file system listener.builder()Creates a new builder.voidChecks whether the file and its children have been created, modified or deleted.voiddestroy()Final processing.Returns the directory being observed.Returns the fileFilter.Returns the set of registered file system listeners.voidInitializes the observer.voidremoveListener(FileAlterationListener listener) Removes a file system listener.toString()Returns a String representation of this observer.
- 
Constructor Details- 
FileAlterationObserverDeprecated.Usebuilder().Constructs an observer for the specified directory.- Parameters:
- directory- the directory to observe.
 
- 
FileAlterationObserverDeprecated.Usebuilder().Constructs an observer for the specified directory and file filter.- Parameters:
- directory- The directory to observe.
- fileFilter- The file filter or null if none.
 
- 
FileAlterationObserverDeprecated.Usebuilder().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.
 
- 
FileAlterationObserverConstructs 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.
 
- 
FileAlterationObserverDeprecated.Usebuilder().Constructs an observer for the specified directory.- Parameters:
- directoryName- the name of the directory to observe.
 
- 
FileAlterationObserverDeprecated.Usebuilder().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@Deprecated public FileAlterationObserver(String directoryName, FileFilter fileFilter, IOCase ioCase) Deprecated.Usebuilder().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- 
builderCreates a new builder.- Returns:
- a new builder.
- Since:
- 2.18.0
 
- 
addListenerAdds a file system listener.- Parameters:
- listener- The file system listener.
 
- 
checkAndNotifyChecks whether the file and its children have been created, modified or deleted.
- 
destroyFinal processing.- Throws:
- Exception- if an error occurs.
 
- 
getDirectoryReturns the directory being observed.- Returns:
- the directory being observed.
 
- 
getFileFilterReturns the fileFilter.- Returns:
- the fileFilter.
- Since:
- 2.1
 
- 
getListenersReturns the set of registered file system listeners.- Returns:
- The file system listeners
 
- 
initializeInitializes the observer.- Throws:
- Exception- if an error occurs.
 
- 
removeListenerRemoves a file system listener.- Parameters:
- listener- The file system listener.
 
- 
toStringReturns a String representation of this observer.
 
- 
builder().