This article uses regular expressions to filter some special files in the folder and print them out. This involves the file filter, which is applied to the anonymous class.
Package date0815.demo1;/** * Java anonymous inner class * @author ZZW * */import java.io.file;import java.io.filenamefilter;import java.ut Il. Arrays;import Java.util.regex.pattern;public class Test1 {public static filenamefilter filter (final String regex) {//anonymous class , directly using an anonymous class to return an object of the FilenameFilter class return new FilenameFilter () {//compile the regular expression with the private pattern pattern = pattern.compile ( regex);//Overwrite the only accept method in the FilenameFilter interface @overridepublic Boolean accept (File dir, String name) {return Pattern.matcher (name). matches ();}};} public static void Main (string[] args) {//Create a new File = "."); /returns the filtered list of files, using regular expressions to filter all files ending with. html string[] fileList = file.list (Filter (". *\\.html"));//Case-insensitive ordering of strings Arrays.sort (FileList, String.case_insensitive_order);//print file name for (String str:filelist) {System.out.println (str);}}}
Examples of anonymous classes in Java