File.listFiles(FilenameFilter filter) has the following syntax.
public File [] listFiles(FilenameFilter filter)
In the following code shows how to use File.listFiles(FilenameFilter filter) method.
//w w w. j a v a2 s .c om import java.io.File; import java.io.FilenameFilter; public class Main { public static void main(String[] args) { File f = new File("c:/test"); FilenameFilter fileNameFilter = new FilenameFilter() { @Override public boolean accept(File dir, String name) { if (name.lastIndexOf('.') > 0) { int lastIndex = name.lastIndexOf('.'); String str = name.substring(lastIndex); if (str.equals(".txt")) { return true; } } return false; } }; File[] paths = f.listFiles(fileNameFilter); for (File path : paths) { System.out.println(path); } } }
The code above generates the following result.