Commons Io 简明教程
Apache Commons IO - WildcardFileFilter
Commons IO 中的 WildcardFileFilter 使用提供的通配符过滤文件。
WildcardFileFilter in Commons IO filters the files using the supplied wildcards.
Class Declaration
以下是 org.apache.commons.io.filefilter.WildcardFileFilter 类的声明:
Following is the declaration for org.apache.commons.io.filefilter.WildcardFileFilter Class −
public class WildcardFileFilter
extends AbstractFileFilter implements Serializable
Example of WildcardFileFilter Class
这里是我们需要解析的输入文件
Here is the input file we need to parse
Welcome to TutorialsPoint. Simply Easy Learning.
让我们打印当前目录中的所有文件和目录,然后过滤名称以 t 结尾的文件。
Let’s print all files and directories in the current directory and then, filter a file whose name ends with t.
IOTester.java
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.filefilter.WildcardFileFilter;
public class IOTester {
public static void main(String[] args) {
try {
usingWildcardFileFilter();
} catch(IOException e) {
System.out.println(e.getMessage());
}
}
public static void usingWildcardFileFilter() throws IOException {
//get the current directory
File currentDirectory = new File(".");
//get names of all files and directory in current directory
String[] files = currentDirectory.list();
System.out.println("All files and Folders.\n");
for( int i = 0; i < files.length; i++ ) {
System.out.println(files[i]);
}
System.out.println("\nFile name ending with t.\n");
String[] filesNames = currentDirectory.list( new WildcardFileFilter("*t"));
for( int i = 0; i < filesNames.length; i++ ) {
System.out.println(filesNames[i]);
}
}
}