public class SearchFile { private static final class OnlyFile implements FileFilter { public boolean accept(File pathname) { if (pathname.isFile()) { return true; } else { return false; } } }
public static void main(String[] args) { if (args.length != 2) { System.out.println("使用说明:请输入java SearchFile 目录 文件名"); } else { File file = new File(args[0]); File[] files = file.listFiles(new OnlyFile()); for (File f : files) { if (f.getName().contains(args[1])) { System.out.println(f.getPath()); } } } } }