文件过滤器

/** * 文件过滤器 * @author Heying * */
public class FileFlterDemo {
    public static void main(String[] args) throws Exception {
        File file = new File("./");

        FileFilter filter = new MyFilter();

        File[] subs = file.listFiles(filter);

        for (File sub : subs) {
            System.out.println(sub);
        }

        new FileTest();
    }

}

class MyFilter implements FileFilter{
    @Override
    public boolean accept(File file) {
        return file.getName().startsWith(".");
    }
}

class FileTest{

    public FileTest() {
        File file = new File(".");

        FileFilter filter = new FileFilter() {
            // 重写规则
            @Override
            public boolean accept(File file) {
                return file.getName().endsWith(".txt");
            }
        };
        File[] files = file.listFiles(filter);

        for (File fileer : files) {
            System.out.println(fileer.getName());
        }

    }
}

你可能感兴趣的:(File,filter)