package chapter7;

import java.io.File;
import java.io.FilenameFilter;

public class Example04 {
    public static void main(String[] args) {
        // 创建 File 对象
        File file = new File("src/chapter7");
        // 创建过滤器对象
        FilenameFilter filter = new FilenameFilter() {
            // 实现 accept 方法
            @Override
            public boolean accept(File dir, String name) {
                File currFile = new File(dir, name);
                if (currFile.isFile() && name.endsWith(".java")) {
                    return true;
                } else {
                    return false;
                }
            }
        };

        if (file.exists() && file.isDirectory()) {
            String[] names = file.list(filter);
            for (String name : names) {
                System.out.println(name);
            }
        }

        if (file.exists() && file.isDirectory()) {
            File[] files = file.listFiles(filter);
            for (File currentFile : files) {
                System.out.println(currentFile);
            }
        }

    }
}
