Re: List of files with given extension in a directory
Hakan <H.L@softhome.net> wrote in news:1249269137.43@user.newsoffice.de:
I would like for the program to find all files with a given extension
in a user chosen directory. The user chooses a directory with a
JFileChooser or something like that and all entries of a certain file
type in there are showed in a drop down list. It seems to be
straightforward to me.
First you define a FilenameFilter :
import java.io.File;
import java.io.FilenameFilter;
public class ExtensionFilter implements FilenameFilter {
protected String pattern;
public Filter (String str) {
pattern = str;
}
public boolean accept (File dir, String name) {
return name.toLowerCase().endsWith(pattern.toLowerCase());
}
}
then you are able to do this :
ExtensionFilter ef = new ExtensionFilter (".txt");
// current directory
File dir = new File ("C:\\mydir");
String[] strs = dir.list(ef);
for (int i = 0; i < strs.length; i++) {
System.out.println (strs[i]);
}
Bye.
--
Real Gagnon from Quebec, Canada
* Java, Javascript, VBScript and PowerBuilder code snippets
* http://www.rgagnon.com/howto.html
* http://www.rgagnon.com/bigindex.html