用endsWith实现后缀名的匹配
package com.data.io; import java.io.File; import java.io.FileFilter; import java.io.FilenameFilter; public class Demo { static class SourceFileFilter implements FileFilter { @Override public boolean accept(File pathname) { return pathname.getAbsolutePath().endsWith(".java") || pathname.isDirectory() || pathname.isHidden() || pathname.getName().endsWith(".c"); } } public static void main(String[] args) { File dir = new File("G:\JAVA protect\C_Course"); // File[] files = dir.listFiles(new SourceFileFilter()); File[] files = dir.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return !name.endsWith(".java"); } }); for(File file: files) { System.out.println(file.getAbsolutePath()); } } }
运行结果