org.pf.file
Class FileFinder

java.lang.Object
  extended by org.pf.file.FileFinder
All Implemented Interfaces:
FileHandler

public class FileFinder
extends java.lang.Object
implements FileHandler

Helper class with convenient methods to find files.

Version:
1.7
Author:
Manfred Duchrow

Method Summary
 boolean directoryEnd(java.io.File dir)
          This method is called for each directory, that a FileWalker finished to walk through.
 boolean directoryStart(java.io.File dir, int count)
          This method is called for each directory, that a FileWalker starts to walk through.
static java.io.File[] findDirectories(java.lang.String startDir, java.lang.String pattern)
          Return all directories that match the given pattern(s) start searching in the specified startDir.
static java.io.File[] findDirectories(java.lang.String startDir, java.lang.String pattern, boolean recursive)
          Return all directories that match the given pattern(s) start searching in the specified startDir.
static java.io.File[] findDirectories(java.lang.String startDir, java.lang.String pattern, boolean recursive, char digitWildcard)
          Return all directories that match the given pattern(s) start searching in the specified startDir.
static java.io.File findFile(java.lang.String filename)
          Tries to find the file with the given Name.
static java.io.File findFileOnClasspath(java.lang.String filename)
          Tries to find the file with the given Name on the classpath.
static java.io.File findFileOnPath(java.lang.String filename, java.lang.String path)
          Tries to find the file with the given name in one location of the given location path.
static java.io.File[] findFiles(java.lang.String searchPattern)
          Return all files that match the given search pattern.
static java.io.File[] findFiles(java.lang.String searchPattern, char digitWildcard)
          Return all files that match the given search pattern.
static java.io.File[] findFiles(java.lang.String dir, java.lang.String pattern)
          Return all files that match the given pattern(s) start searching in the specified dir.
static java.io.File[] findFiles(java.lang.String dir, java.lang.String pattern, boolean recursive)
          Return all files that match the given pattern(s) start searching in the specified dir.
static java.io.File[] findFiles(java.lang.String dir, java.lang.String pattern, boolean recursive, char digitWildcard)
          Return all files that match the given pattern(s) start searching in the specified dir.
 boolean handleException(java.lang.Exception ex, java.io.File file)
          This method is called for whenever an exception occurs in walking through the directories.
 boolean handleFile(java.io.File file)
          This method is called for each file, that a FileWalker instance finds.
static java.net.URL locateFile(java.lang.String filename)
          Tries to find the file with the given Name.
static java.net.URL locateFileOnClasspath(java.lang.String filename)
          Tries to find the file with the given Name on the classpath.
static java.net.URL locateFileOnPath(java.lang.String filename, Classpath path)
          Tries to find the file with the given name in one location of the given location path.
static java.net.URL locateFileOnPath(java.lang.String filename, java.lang.String path)
          Tries to find the file with the given name in one location of the given location path.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

locateFile

public static java.net.URL locateFile(java.lang.String filename)
Tries to find the file with the given Name. First it checks if the file exists directly under the given name in the current work directory. If not, it searches the classpath to find it. If the file was found and really exists, then its URL will be returned. In all other cases null will be returned.


locateFileOnClasspath

public static java.net.URL locateFileOnClasspath(java.lang.String filename)
Tries to find the file with the given Name on the classpath. If the file was found its URL will be returned. In all other cases null will be returned.

Parameters:
filename - The name of the file to look for

findFileOnClasspath

public static java.io.File findFileOnClasspath(java.lang.String filename)
Tries to find the file with the given Name on the classpath. If the file was found and really exists, then it will be returned. In all other cases null will be returned.


locateFileOnPath

public static java.net.URL locateFileOnPath(java.lang.String filename,
                                            java.lang.String path)
Tries to find the file with the given name in one location of the given location path. The location path is like the Java classpath a concatenation of file directories and archives. The separator could be ':' or ';' depending on current platform.

Parameters:
filename - The name of the file to look for
path - The path to look up for the file (e.g. "xxx.jar:test/lib:root.jar")

locateFileOnPath

public static java.net.URL locateFileOnPath(java.lang.String filename,
                                            Classpath path)
Tries to find the file with the given name in one location of the given location path. The location path is like the Java classpath a concatenation of file directories and archives. The separator could be ':' or ';' depending on current platform.

Parameters:
filename - The name of the file to look for
path - The path to look up for the file (e.g. "xxx.jar:test/lib:root.jar")

findFileOnPath

public static java.io.File findFileOnPath(java.lang.String filename,
                                          java.lang.String path)
Tries to find the file with the given name in one location of the given location path. The location path is like the Java classpath a concatenation of file directories and archives. The separator could be ':' or ';'.


findFile

public static java.io.File findFile(java.lang.String filename)
Tries to find the file with the given Name. First it looks if the file exists directly under the given name. If not, it searches the classpath to find it. If the file was found and really exists, then it will be returned. In all other cases null will be returned.


findFiles

public static java.io.File[] findFiles(java.lang.String dir,
                                       java.lang.String pattern)
Return all files that match the given pattern(s) start searching in the specified dir. Searches in all sub directories as well. More than one pattern can be specified in parameter pattern. They have to be separated by ';'.

Parameters:
dir - The directory to start searching (must not be null)
pattern - The pattern(s) the filenames must match (must not be null )
Returns:
All file found that matched to at least one of the patterns
Throws:
java.lang.IllegalArgumentException - If dir or pattern is null

findFiles

public static java.io.File[] findFiles(java.lang.String dir,
                                       java.lang.String pattern,
                                       boolean recursive)
Return all files that match the given pattern(s) start searching in the specified dir. Look into sub directories if recursive is true. More than one pattern can be specified in parameter pattern. They have to be separated by ';'.

Parameters:
dir - The directory to start searching (must not be null)
pattern - The pattern(s) the filenames must match (must not be null )
recursive - If false, only dir is searched, otherwise all sub directories as well
Returns:
All file found that matched to at least one of the patterns
Throws:
java.lang.IllegalArgumentException - If dir or pattern is null

findFiles

public static java.io.File[] findFiles(java.lang.String dir,
                                       java.lang.String pattern,
                                       boolean recursive,
                                       char digitWildcard)
Return all files that match the given pattern(s) start searching in the specified dir. Look into sub directories if recursive is true. Use the given digit wildcard in patterns to match single digits in filenames.
More than one pattern can be specified in parameter pattern. They have to be separated by ';'.

Parameters:
dir - The directory to start searching (must not be null)
pattern - The pattern(s) the filenames must match (must not be null )
recursive - If false, only dir is searched, otherwise all sub directories as well
digitWildcard - The wildcard character for digit representation in the pattern(s)
Returns:
All file found that matched to at least one of the patterns
Throws:
java.lang.IllegalArgumentException - If dir or pattern is null

findFiles

public static java.io.File[] findFiles(java.lang.String searchPattern)
Return all files that match the given search pattern. The pattern may contain '**' as last path element to indicate a recursive search.
Example: "/usr/test\/**\/*.gz"

Parameters:
searchPattern - The pattern containing path and filename pattern the files must match (must not be null )
Returns:
All files found that matched the pattern
Throws:
java.lang.IllegalArgumentException - If searchPattern is null

findFiles

public static java.io.File[] findFiles(java.lang.String searchPattern,
                                       char digitWildcard)
Return all files that match the given search pattern. The pattern may contain '**' as last path element to indicate a recursive search.
Example: "/usr/test\/**\/*.gz"

Parameters:
searchPattern - The pattern containing path and filename pattern the files must match (must not be null )
digitWildcard - The wildcard character for digit representation in the pattern(s)
Returns:
All files found that matched the pattern
Throws:
java.lang.IllegalArgumentException - If searchPattern is null

findDirectories

public static java.io.File[] findDirectories(java.lang.String startDir,
                                             java.lang.String pattern,
                                             boolean recursive,
                                             char digitWildcard)
Return all directories that match the given pattern(s) start searching in the specified startDir. Look into each sub directories if recursive is true. Use the given digit wildcard in patterns to match single digits in filenames.
More than one pattern can be specified in parameter pattern. They have to be separated by ';'.

Parameters:
startDir - The directory to start searching (must not be null)
pattern - The pattern(s) the directorynames must match (must not be null )
recursive - If false, only startDir is searched, otherwise all sub directories as well
digitWildcard - The wildcard character for digit representation in the pattern(s)
Returns:
All directories found that matched to at least one of the patterns
Throws:
java.lang.IllegalArgumentException - If dir or pattern is null

findDirectories

public static java.io.File[] findDirectories(java.lang.String startDir,
                                             java.lang.String pattern,
                                             boolean recursive)
Return all directories that match the given pattern(s) start searching in the specified startDir. Look into each sub directories if recursive is true. More than one pattern can be specified in parameter pattern. They have to be separated by ';'.

Parameters:
startDir - The directory to start searching (must not be null)
pattern - The pattern(s) the directorynames must match (must not be null )
recursive - If false, only startDir is searched, otherwise all sub directories as well
Returns:
All directories found that matched to at least one of the patterns
Throws:
java.lang.IllegalArgumentException - If dir or pattern is null

findDirectories

public static java.io.File[] findDirectories(java.lang.String startDir,
                                             java.lang.String pattern)
Return all directories that match the given pattern(s) start searching in the specified startDir. This method also searches in each sub directory. More than one pattern can be specified in parameter pattern. They have to be separated by ';'.

Parameters:
startDir - The directory to start searching (must not be null)
pattern - The pattern(s) the directorynames must match (must not be null )
Returns:
All directories found that matched to at least one of the patterns
Throws:
java.lang.IllegalArgumentException - If dir or pattern is null

handleFile

public boolean handleFile(java.io.File file)
This method is called for each file, that a FileWalker instance finds. It must return true, if the FileWalker should continue. To stop the calling FileWalker it can return false.

Specified by:
handleFile in interface FileHandler
Parameters:
file - The file, currently found by the FileWalker instance
Returns:
true to continue, false to terminate processing of files

handleException

public boolean handleException(java.lang.Exception ex,
                               java.io.File file)
This method is called for whenever an exception occurs in walking through the directories.
The method must return true, if the FileWalker should continue. To stop the calling FileWalker it can return false.

Specified by:
handleException in interface FileHandler
Parameters:
ex - The exception to handle
file - The file, currently found by the FileWalker instance
Returns:
true to continue, false to terminate processing of files

directoryEnd

public boolean directoryEnd(java.io.File dir)
This method is called for each directory, that a FileWalker finished to walk through. It must return true, if the FileWalker should continue. To stop the calling FileWalker it can return false.

Specified by:
directoryEnd in interface FileHandler
Parameters:
dir - The directory, the FileWalker has finished to walk through
Returns:
true to continue, false to terminate processing of files

directoryStart

public boolean directoryStart(java.io.File dir,
                              int count)
This method is called for each directory, that a FileWalker starts to walk through. It must return true, if the FileWalker should continue. To stop the calling FileWalker it can return false.

Specified by:
directoryStart in interface FileHandler
Parameters:
dir - The directory, the FileWalker is starting to walk through
count - The number of files and directories the FileWalker found in the directory
Returns:
true to continue, false to terminate processing of files