Implementation effect: List the files with specific suffix names in a certain directory (for example, list the files with txt suffixes in the root directory of the D disk)
import java.io.File;import java.io.FilenameFilter;public class QueryFile implements FilenameFilter{String extension; //File extension public QueryFile(String extension){this.extension="."+extension.toLowerCase(); //File extension standardization}@Overridepublic boolean accept(File dir, String name) { //Rewrite the method of FilenameFilter interface File file=new File(dir,name);if(file.getName().toLowerCase().endsWith(extension)){return true;}return false;}public static void main(String[] args) {File file=new File("D:/");QueryFile query=new QueryFile("txt"); //File with the suffix txt String[] array=file.list(query); //Filter starts filtering for(int i=0;i<array.length;i++){System.out.println(array[i]);}}}illustrate:
FilenameFilter interface is an interface used to filter files, and the same interface is FileFilter
The above is the entire content of the IO file suffix filtering summary shared this time. If you still don’t understand, you can refer to the following related articles, or directly discuss in the comment area below. Thank you for your support for Wulin.com.