These criteria consist of a set of include and exclude patterns. With these patterns, you can select which files you want to have included, and which files you want to have excluded.
The idea is simple. A given directory is recursively scanned for all files and directories. Each file/directory is matched against a set of include and exclude patterns. Only files/directories that match at least one pattern of the include pattern list, and don't match a pattern of the exclude pattern list will be placed in the list of files/directories found.
When no list of include patterns is supplied, "**" will be used, which means that everything will be matched. When no list of exclude patterns is supplied, an empty list is used, such that nothing will be excluded.
The pattern matching is done as follows: The name to be matched is split up in path segments. A path segment is the name of a directory or file, which is bounded by File.separator
('/' under UNIX, '\' under Windows). E.g. "abc/def/ghi/xyz.java" is split up in the segments "abc", "def", "ghi" and "xyz.java". The same is done for the pattern against which should be matched.
Then the segments of the name and the pattern will be matched against each other. When '**' is used for a path segment in the pattern, then it matches zero or more path segments of the name.
There are special case regarding the use of File.separator
s at the beginningof the pattern and the string to match:
When a pattern starts with a File.separator
, the string to match must also start with a File.separator
. When a pattern does not start with a File.separator
, the string to match may not start with a File.separator
. When one of these rules is not obeyed, the string will not match.
When a name path segment is matched against a pattern path segment, the following special characters can be used: '*' matches zero or more characters, '?' matches one character.
Examples:
"**\*.class" matches all .class files/dirs in a directory tree.
"test\a??.java" matches all files/dirs which start with an 'a', then two more characters and then ".java", in a directory called test.
"**" matches everything in a directory tree.
"**\test\**\XYZ*" matches all files/dirs that start with "XYZ" and where there is a parent directory called test (e.g. "abc\test\def\ghi\XYZ123").
Example of usage:
String[] includes = {"**\\*.class"}; String[] excludes = {"modules\\*\\**"}; ds.setIncludes(includes); ds.setExcludes(excludes); ds.setBasedir(new File("test")); ds.scan(); System.out.println("FILES:"); String[] files = ds.getIncludedFiles(); for (int i = 0; i < files.length;i++) { System.out.println(files[i]); }This will scan a directory called test for .class files, but excludes all .class files in all directories under a directory called "modules"
|
|
|
|