I've been strugling with a problem connected with FilenameFilter. I'd like to pass an object of my class implementing FilenameFilter into a method which recursively goes through files into a folder tree. A version of this method without FilenameFilter object passed as an argument works fine, but overloaded version doesn't. The problem is that it doesn't go recursively into a folder tree, though it should. Maybe a possible problem is related to arguments of the accept(File dir, String name); method, there are two arguments and I'm not pretty sure if and how I should pass values to them... I'll be very grateful for any help... ps. The method listfilesRecursive(FilenameFilter filter) works on an object of a class MyFile with one argument and thus one field path
public class MyFile {
String path;
public MyFile (String path) {
this.path = path;
}
List <File> list = new ArrayList<File> ();
List <String> nameList = new ArrayList <String> ();
public List <File> listFilesRecursive(FilenameFilter fFilter) {
File f1 = new File(path);
File [] files = f1.listFiles(fFilter);
for (File fil : files) {
if (fil.isFile()) {
list.add(fil);
nameList.add(fil.getName());
} else if (fil.isDirectory()) {
this.path = fil.toString();
this.listFilesRecursive(fFilter);
}
}
Collections.sort(nameList);
return list;
}
and below is the class implementing FilenameFilter:
class MyFilenameFilter implements FilenameFilter {
String ext;
public MyFilenameFilter (String ext) {
this.ext = ext;
}
@Override
public boolean accept(File dir, String name) {
// System.out.println(name + " <<<<<<<<<<");
if(name.lastIndexOf('.')>0)
{
int lastIndex = name.lastIndexOf('.');
String str = name.substring(lastIndex);
if(str.equals(ext))
{
return true;
}
}
return false;
}
}
And finally, will FilenameFilter a good choice if I'd like to implement a method going though folders selecting them and filtring them accoring to their names?