|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * This package defines an interface (IOFileFilter) that combines both |
| 20 | + * {@link java.io.FileFilter} and {@link java.io.FilenameFilter}. |
| 21 | + * <p>Besides that the package offers a series of ready-to-use implementations of the |
| 22 | + * IOFileFilter interface including implementation that allow you to combine |
| 23 | + * other such filters.</p> |
| 24 | + * <p>These filter can be used to list files or in {@link java.awt.FileDialog}, |
| 25 | + * for example.</p> |
| 26 | + * <table> |
| 27 | + * <caption>There are a number of 'primitive' filters:</caption> |
| 28 | + * <tbody> |
| 29 | + * <tr> |
| 30 | + * <td><a href="DirectoryFileFilter.html">DirectoryFilter</a></td> |
| 31 | + * <td>Only accept directories</td> |
| 32 | + * </tr> |
| 33 | + * <tr> |
| 34 | + * <td><a href="PrefixFileFilter.html">PrefixFileFilter</a></td> |
| 35 | + * <td>Filter based on a prefix</td> |
| 36 | + * </tr> |
| 37 | + * <tr> |
| 38 | + * <td><a href="SuffixFileFilter.html">SuffixFileFilter</a></td> |
| 39 | + * <td>Filter based on a suffix</td> |
| 40 | + * </tr> |
| 41 | + * <tr> |
| 42 | + * <td><a href="NameFileFilter.html">NameFileFilter</a></td> |
| 43 | + * <td>Filter based on a filename</td> |
| 44 | + * </tr> |
| 45 | + * <tr> |
| 46 | + * <td><a href="WildcardFileFilter.html">WildcardFileFilter</a></td> |
| 47 | + * <td>Filter based on wildcards</td> |
| 48 | + * </tr> |
| 49 | + * <tr> |
| 50 | + * <td><a href="AgeFileFilter.html">AgeFileFilter</a></td> |
| 51 | + * <td>Filter based on last modified time of file</td> |
| 52 | + * </tr> |
| 53 | + * <tr> |
| 54 | + * <td><a href="SizeFileFilter.html">SizeFileFilter</a></td> |
| 55 | + * <td>Filter based on file size</td> |
| 56 | + * </tr> |
| 57 | + * </tbody> |
| 58 | + * </table> |
| 59 | + * <table> |
| 60 | + * <caption>And there are five 'boolean' filters:</caption> |
| 61 | + * <tbody> |
| 62 | + * <tr> |
| 63 | + * <td><a href="TrueFileFilter.html">TrueFileFilter</a></td> |
| 64 | + * <td>Accept all files</td> |
| 65 | + * </tr> |
| 66 | + * <tr> |
| 67 | + * <td><a href="FalseFileFilter.html">FalseFileFilter</a></td> |
| 68 | + * <td>Accept no files</td> |
| 69 | + * </tr> |
| 70 | + * <tr> |
| 71 | + * <td><a href="NotFileFilter.html">NotFileFilter</a></td> |
| 72 | + * <td>Applies a logical NOT to an existing filter</td> |
| 73 | + * </tr> |
| 74 | + * <tr> |
| 75 | + * <td><a href="AndFileFilter.html">AndFileFilter</a></td> |
| 76 | + * <td>Combines two filters using a logical AND</td> |
| 77 | + * </tr> |
| 78 | + * <tr> |
| 79 | + * <td><a href="OrFileFilter.html">OrFileFilter</a></td> |
| 80 | + * <td>Combines two filter using a logical OR</td> |
| 81 | + * </tr> |
| 82 | + * |
| 83 | + * </tbody> |
| 84 | + * </table> |
| 85 | + * <h2>Using Classic IO</h2> |
| 86 | + * <p>These boolean FilenameFilters can be nested, to allow arbitrary expressions. |
| 87 | + * For example, here is how one could print all non-directory files in the |
| 88 | + * current directory, starting with "A", and ending in ".java" or ".class":</p> |
| 89 | + * |
| 90 | + * <pre> |
| 91 | + * File dir = new File("."); |
| 92 | + * String[] files = dir.list( |
| 93 | + * new AndFileFilter( |
| 94 | + * new AndFileFilter( |
| 95 | + * new PrefixFileFilter("A"), |
| 96 | + * new OrFileFilter( |
| 97 | + * new SuffixFileFilter(".class"), |
| 98 | + * new SuffixFileFilter(".java") |
| 99 | + * ) |
| 100 | + * ), |
| 101 | + * new NotFileFilter( |
| 102 | + * new DirectoryFileFilter() |
| 103 | + * ) |
| 104 | + * ) |
| 105 | + * ); |
| 106 | + * for (int i=0; i<files.length; i++) { |
| 107 | + * System.out.println(files[i]); |
| 108 | + * } |
| 109 | + * </pre> |
| 110 | + * <p> |
| 111 | + * You can alternatively build a filter tree using the "and", "or", and "not" methods on filters themselves: |
| 112 | + * </p> |
| 113 | + * <pre> |
| 114 | + * File dir = new File("."); |
| 115 | + * String[] files = dir.list( |
| 116 | + * new AndFileFilter( |
| 117 | + * new PrefixFileFilter("A").and( |
| 118 | + * new SuffixFileFilter(".class").or(new SuffixFileFilter(".java"))), |
| 119 | + * new DirectoryFileFilter().not() |
| 120 | + * ) |
| 121 | + * ); |
| 122 | + * for (int i=0; i<files.length; i++) { |
| 123 | + * System.out.println(files[i]); |
| 124 | + * } |
| 125 | + * </pre> |
| 126 | + * <p>This package also contains a utility class: |
| 127 | + * <a href="FileFilterUtils.html">FileFilterUtils</a>. It allows you to use all |
| 128 | + * file filters without having to put them in the import section. Here's how the |
| 129 | + * above example will look using FileFilterUtils:</p> |
| 130 | + * <pre> |
| 131 | + * File dir = new File("."); |
| 132 | + * String[] files = dir.list( |
| 133 | + * FileFilterUtils.andFileFilter( |
| 134 | + * FileFilterUtils.andFileFilter( |
| 135 | + * FileFilterUtils.prefixFileFilter("A"), |
| 136 | + * FileFilterUtils.orFileFilter( |
| 137 | + * FileFilterUtils.suffixFileFilter(".class"), |
| 138 | + * FileFilterUtils.suffixFileFilter(".java") |
| 139 | + * ) |
| 140 | + * ), |
| 141 | + * FileFilterUtils.notFileFilter( |
| 142 | + * FileFilterUtils.directoryFileFilter() |
| 143 | + * ) |
| 144 | + * ) |
| 145 | + * ); |
| 146 | + * for (int i=0; i<files.length; i++) { |
| 147 | + * System.out.println(files[i]); |
| 148 | + * } |
| 149 | + * </pre> |
| 150 | + * <h2>Using NIO</h2> |
| 151 | + * <p>You can combine Java <b>file tree walking</b> by using <code>java.nio.file.Files.walk()</code> APIs with filters:</p> |
| 152 | + * <pre> |
| 153 | + * final Path dir = Paths.get(""); |
| 154 | + * // We are interested in files older than one day |
| 155 | + * final long cutoff = System.currentTimeMillis() - (24 * 60 * 60 * 1000); |
| 156 | + * final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(new AgeFileFilter(cutoff)); |
| 157 | + * // |
| 158 | + * // Walk one dir |
| 159 | + * Files.<b>walkFileTree</b>(dir, Collections.emptySet(), 1, visitor); |
| 160 | + * System.out.println(visitor.getPathCounters()); |
| 161 | + * System.out.println(visitor.getFileList()); |
| 162 | + * // |
| 163 | + * visitor.getPathCounters().reset(); |
| 164 | + * // |
| 165 | + * // Walk dir tree |
| 166 | + * Files.<b>walkFileTree</b>(dir, visitor); |
| 167 | + * System.out.println(visitor.getPathCounters()); |
| 168 | + * System.out.println(visitor.getDirList()); |
| 169 | + * System.out.println(visitor.getFileList()); |
| 170 | + * </pre> |
| 171 | + * <p>There are a few other goodies in that class so please have a look at the |
| 172 | + * documentation in detail.</p> |
| 173 | + */ |
| 174 | +package org.apache.commons.io.filefilter; |
0 commit comments