List files of a certain type
import java.io.File;
import java.io.FilenameFilter;
public class Main {
public static void main(String[] args) {
File directory = new File("c:\\");
if (!directory.isDirectory()) {
System.out.println("No directory provided");
return;
}
FilenameFilter filefilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".txt");
}
};
String[] filenames = directory.list(filefilter);
for (String name : filenames) {
System.out.println(name);
}
}
}
Related examples in the same category