package com.zsh.test; import java.io.File; import java.io.FileFilter; public class Test implements FileFilter { /** * @param args * @author Ben Zeph */ String condition = ""; public Test(String condition) { this.condition = condition; } @Override public boolean accept(File pathname) { String filename = pathname.getName(); if (filename.lastIndexOf(condition) != -1) { return true; } else return false; } public static void main(String[] args) { File root = new File("C://"); File[] files = root.listFiles(new Test(".txt")); if (files.length != 0) for (int i = 0; i < files.length; i++) System.out.println(files[i]); } }