This class operates with a FileFilter and maximum depth to
limit the files and direcories visited.
Commons IO supplies many common filter implementations in the
filefilter package.
The following sections describe:
FileCleaner implementation.FileFilter(s) with DirectoryWalker.
public class FileCleaner extends DirectoryWalker {
public FileCleaner() {
super();
}
public List clean(File startDirectory) {
List results = new ArrayList();
walk(startDirectory, results);
return results;
}
protected boolean handleDirectory(File directory, int depth, Collection results) {
// delete svn directories and then skip
if (".svn".equals(directory.getName())) {
directory.delete();
return false;
} else {
return true;
}
}
protected void handleFile(File file, int depth, Collection results) {
// delete file and add to list of deleted
file.delete();
results.add(file);
}
}
The first option is to visit all directories and files. This is achieved via the no-args constructor.
The second constructor option is to supply a single FileFilter
that describes the files and directories to visit. Care must be taken
with this option as the same filter is used for both directories
and files.
For example, if you wanted all directories which are not hidden and files which end in ".txt":
public class FooDirectoryWalker extends DirectoryWalker {
public FooDirectoryWalker(FileFilter filter) {
super(filter, -1);
}
}
// Build up the filters and create the walker
// Create a filter for Non-hidden directories
IOFileFilter fooDirFilter =
FileFilterUtils.andFileFilter(FileFilterUtils.directoryFileFilter,
HiddenFileFilter.VISIBLE);
// Create a filter for Files ending in ".txt"
IOFileFilter fooFileFilter =
FileFilterUtils.andFileFilter(FileFilterUtils.fileFileFilter,
FileFilterUtils.suffixFileFilter(".txt"));
// Combine the directory and file filters using an OR condition
java.io.FileFilter fooFilter =
FileFilterUtils.orFileFilter(fooDirFilter, fooFileFilter);
// Use the filter to construct a DirectoryWalker implementation
FooDirectoryWalker walker = new FooDirectoryWalker(fooFilter);
The third constructor option is to specify separate filters, one for
directories and one for files. These are combined internally to form
the correct FileFilter, something which is very easy to
get wrong when attempted manually, particularly when trying to
express constructs like 'any file in directories named docs'.
For example, if you wanted all directories which are not hidden and files which end in ".txt":
public class FooDirectoryWalker extends DirectoryWalker {
public FooDirectoryWalker(IOFileFilter dirFilter, IOFileFilter fileFilter) {
super(dirFilter, fileFilter, -1);
}
}
// Use the filters to construct the walker
FooDirectoryWalker walker = new FooDirectoryWalker(
HiddenFileFilter.VISIBLE,
FileFilterUtils.suffixFileFilter(".txt"),
);
This is much simpler than the previous example, and is why it is the preferred
option for filtering.
What DirectoryWalker does provide for cancellation is:
CancelException which can be thrown in any of the
lifecycle methods to stop processing.walk() method traps thrown CancelException
and calls the handleCancelled() method, providing
a place for custom cancel processing.Implementations need to provide:
CancelException.handleCancelled() method.
Two possible scenarios are envisaged for cancellation:
The following sections provide example implementations for these two different scenarios.
cancel() method that can be
called by another thread to stop the processing. A typical example use-case
would be a cancel button on a GUI. Calling this method sets a
volatile flag to ensure it will work properly in a multi-threaded environment.
The flag is returned by the handleIsCancelled() method, which
will cause the walk to stop immediately. The handleCancelled()
method will be the next, and last, callback method received once cancellation
has occurred.
public class FooDirectoryWalker extends DirectoryWalker {
private volatile boolean cancelled = false;
public void cancel() {
cancelled = true;
}
private void handleIsCancelled(File file, int depth, Collection results) {
return cancelled;
}
protected void handleCancelled(File startDirectory, Collection results, CancelException cancel) {
// implement processing required when a cancellation occurs
}
}
Note
the decision logic and throwing a CancelException could be implemented
in any of the lifecycle methods.
public class BarDirectoryWalker extends DirectoryWalker {
protected boolean handleDirectory(File directory, int depth, Collection results) throws IOException {
// cancel if hidden directory
if (directory.isHidden()) {
throw new CancelException(file, depth);
}
return true;
}
protected void handleFile(File file, int depth, Collection results) throws IOException {
// cancel if read-only file
if (!file.canWrite()) {
throw new CancelException(file, depth);
}
results.add(file);
}
protected void handleCancelled(File startDirectory, Collection results, CancelException cancel) {
// implement processing required when a cancellation occurs
}
}
| Modifier and Type | Class and Description |
|---|---|
| public static class | DirectoryWalker.CancelException
CancelException is thrown in DirectoryWalker to cancel the current processing. |
| Modifier and Type | Field and Description |
|---|---|
| private final int | depthLimit
The limit on the directory depth to walk. |
| private final FileFilter | filter
The file filter to use to filter files and directories. |
| Access | Constructor and Description |
|---|---|
| protected | |
| protected | DirectoryWalker(FileFilter
the filter to apply, null means visit all files filter, int controls how deep the hierarchy is
navigated to (less than 0 means unlimited) depthLimit)Construct an instance with a filter and limit the depth navigated to. |
| protected | DirectoryWalker(IOFileFilter
the filter to apply to directories, null means visit all directories directoryFilter, IOFileFilter the filter to apply to files, null means visit all files fileFilter, int controls how deep the hierarchy is
navigated to (less than 0 means unlimited) depthLimit)Construct an instance with a directory and a file filter and an optional limit on the depth navigated to. |
| Modifier and Type | Method and Description |
|---|---|
| protected final void | checkIfCancelled(File
the current file being processed file, int the current file level (starting directory = 0) depth, Collection<E> the collection of result objects, may be updated results)Checks whether the walk has been cancelled by calling |
| protected void | handleCancelled(File
the directory that the walk started from startDirectory, Collection<E> the collection of result objects, may be updated results, DirectoryWalker.the exception throw to cancel further processing
containing details at the point of cancellation. cancelOverridable callback method invoked when the operation is cancelled. |
| protected boolean | Returns: true to process this directory, false to skip this directorythe current directory being processed directory, int the current directory level (starting directory = 0) depth, Collection<E> the collection of result objects, may be updated results)Overridable callback method invoked to determine if a directory should be processed. |
| protected void | handleDirectoryEnd(File
the directory being processed directory, int the current directory level (starting directory = 0) depth, Collection<E> the collection of result objects, may be updated results)Overridable callback method invoked at the end of processing each directory. |
| protected void | handleDirectoryStart(File
the current directory being processed directory, int the current directory level (starting directory = 0) depth, Collection<E> the collection of result objects, may be updated results)Overridable callback method invoked at the start of processing each directory. |
| protected void | handleEnd(Collection<E>
the collection of result objects, may be updated results)Overridable callback method invoked at the end of processing. |
| protected void | handleFile(File
the current file being processed file, int the current directory level (starting directory = 0) depth, Collection<E> the collection of result objects, may be updated results)Overridable callback method invoked for each (non-directory) file. |
| protected boolean | Returns: true if the walk has been cancelledthe file or directory being processed file, int the current directory level (starting directory = 0) depth, Collection<E> the collection of result objects, may be updated results)Overridable callback method invoked to determine if the entire walk operation should be immediately cancelled. |
| protected void | handleRestricted(File
the restricted directory directory, int the current directory level (starting directory = 0) depth, Collection<E> the collection of result objects, may be updated results)Overridable callback method invoked for each restricted directory. |
| protected void | handleStart(File
the directory to start from startDirectory, Collection<E> the collection of result objects, may be updated results)Overridable callback method invoked at the start of processing. |
| protected final void | walk(File
the directory to start from, not null startDirectory, Collection<E> the collection of result objects, may be updated results)Internal method that walks the directory hierarchy in a depth-first manner. |
| private void | walk(File
the directory to examine, not null directory, int the directory level (starting directory = 0) depth, Collection<E> the collection of result objects, may be updated results)Main recursive method to examine the directory hierarchy. |
| depthLimit | back to summary |
|---|---|
| private final int depthLimit The limit on the directory depth to walk. | |
| filter | back to summary |
|---|---|
| private final FileFilter filter The file filter to use to filter files and directories. | |
| DirectoryWalker | back to summary |
|---|---|
| protected DirectoryWalker() Construct an instance with no filtering and unlimited depth. | |
| DirectoryWalker | back to summary |
|---|---|
| protected DirectoryWalker(FileFilter filter, int depthLimit) Construct an instance with a filter and limit the depth navigated to.
The filter controls which files and directories will be navigated to as
part of the walk. The
| |
| DirectoryWalker | back to summary |
|---|---|
| protected DirectoryWalker(IOFileFilter directoryFilter, IOFileFilter fileFilter, int depthLimit) Construct an instance with a directory and a file filter and an optional limit on the depth navigated to.
The filters control which files and directories will be navigated to as part
of the walk. This constructor uses
| |
| checkIfCancelled | back to summary |
|---|---|
| protected final void checkIfCancelled(File file, int depth, Collection<E> results) throws IOException Checks whether the walk has been cancelled by calling
Writers of subclasses should not normally call this method as it is called
automatically by the walk of the tree. However, sometimes a single method,
typically
| |
| handleCancelled | back to summary |
|---|---|
| protected void handleCancelled(File startDirectory, Collection<E> results, DirectoryWalker. Overridable callback method invoked when the operation is cancelled. The file being processed when the cancellation occurred can be obtained from the exception.
This implementation just re-throws the
| |
| handleDirectory | back to summary |
|---|---|
| protected boolean handleDirectory(File directory, int depth, Collection<E> results) throws IOException Overridable callback method invoked to determine if a directory should be processed. This method returns a boolean to indicate if the directory should be examined or not. If you return false, the entire directory and any subdirectories will be skipped. Note that this functionality is in addition to the filtering by file filter. This implementation does nothing and returns true.
| |
| handleDirectoryEnd | back to summary |
|---|---|
| protected void handleDirectoryEnd(File directory, int depth, Collection<E> results) throws IOException Overridable callback method invoked at the end of processing each directory. This implementation does nothing.
| |
| handleDirectoryStart | back to summary |
|---|---|
| protected void handleDirectoryStart(File directory, int depth, Collection<E> results) throws IOException Overridable callback method invoked at the start of processing each directory. This implementation does nothing.
| |
| handleEnd | back to summary |
|---|---|
| protected void handleEnd(Collection<E> results) throws IOException Overridable callback method invoked at the end of processing. This implementation does nothing.
| |
| handleFile | back to summary |
|---|---|
| protected void handleFile(File file, int depth, Collection<E> results) throws IOException Overridable callback method invoked for each (non-directory) file. This implementation does nothing.
| |
| handleIsCancelled | back to summary |
|---|---|
| protected boolean handleIsCancelled(File file, int depth, Collection<E> results) throws IOException Overridable callback method invoked to determine if the entire walk operation should be immediately cancelled.
This method should be implemented by those subclasses that want to
provide a public
public class FooDirectoryWalker extends DirectoryWalker {
private volatile boolean cancelled = false;
public void cancel() {
cancelled = true;
}
private void handleIsCancelled(File file, int depth, Collection results) {
return cancelled;
}
protected void handleCancelled(File startDirectory,
Collection results, CancelException cancel) {
// implement processing required when a cancellation occurs
}
}
If this method returns true, then the directory walk is immediately
cancelled. The next callback method will be This implementation returns false.
| |
| handleRestricted | back to summary |
|---|---|
| protected void handleRestricted(File directory, int depth, Collection<E> results) throws IOException Overridable callback method invoked for each restricted directory. This implementation does nothing.
| |
| handleStart | back to summary |
|---|---|
| protected void handleStart(File startDirectory, Collection<E> results) throws IOException Overridable callback method invoked at the start of processing. This implementation does nothing.
| |
| walk | back to summary |
|---|---|
| protected final void walk(File startDirectory, Collection<E> results) throws IOException Internal method that walks the directory hierarchy in a depth-first manner. Users of this class do not need to call this method. This method will be called automatically by another (public) method on the specific subclass.
Writers of subclasses should call this method to start the directory walk.
Once called, this method will emit events as it walks the hierarchy.
The event methods have the prefix
| |
| walk | back to summary |
|---|---|
| private void walk(File directory, int depth, Collection<E> results) throws IOException Main recursive method to examine the directory hierarchy.
| |
| Modifier and Type | Field and Description |
|---|---|
| private int | depth
Hides java. |
| private File | file
The file being processed when the exception was thrown. |
| private static final long | serialVersionUID
Serialization id.
|
| Access | Constructor and Description |
|---|---|
| public | CancelException(File
the file when the operation was cancelled, may be null file, int the depth when the operation was cancelled, may be null depth)Constructs a |
| public | CancelException(String
the detail message message, File the file when the operation was cancelled file, int the depth when the operation was cancelled depth)Constructs a |
| Modifier and Type | Method and Description |
|---|---|
| public int | Returns: the depth when the operation was cancelledReturn the depth when the operation was cancelled. |
| public File | Returns: the file when the operation was cancelledReturn the file when the operation was cancelled. |
| depth | back to summary |
|---|---|
| private int depth Hides java. The file depth when the exception was thrown. | |
| file | back to summary |
|---|---|
| private File file The file being processed when the exception was thrown. | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Hides java. Serialization id. | |
| CancelException | back to summary |
|---|---|
| public CancelException(File file, int depth) Constructs a
| |
| CancelException | back to summary |
|---|---|
| public CancelException(String message, File file, int depth) Constructs a | |
| getDepth | back to summary |
|---|---|
| public int getDepth() Return the depth when the operation was cancelled.
| |
| getFile | back to summary |
|---|---|
| public File getFile() Return the file when the operation was cancelled.
| |