tags:

views:

89

answers:

1

I'm trying to get all subdirectories doing this:

   File repositoryDir = new File(_props.getProperty("files.repository.path"));

IOFileFilter filter = new IOFileFilter() {
      public boolean accept(File file) {
          //return file.isDirectory();
       return true;
      }

   @Override
   public boolean accept(File arg0_, String arg1_) {
    // TODO Auto-generated method stub
    return false;
   }
  };

FileUtils.listFiles(repositoryDir,filter,null);

Unfortunately the List returned is empty.

On the other hand if I do this it works:

File[] mainLevelFiles = repositoryDir.listFiles();

Anyone has a clue on what wrong with Apaches FileUtils.listFiles(...)?

A: 

The problem is that you have two accept methods, one returning true and one returning false. Remove the one with the extra string, so that it looks like this:

IOFileFilter filter = new AbstractFileFilter() {
  public boolean accept(File file) {
    return file.isDirectory();
  }
};

Alternatively, you could use Apache's DirectoryFileFilter:

 File dir = new File(_props.getProperty("files.repository.path"));
 String[] files = dir.list( DirectoryFileFilter.INSTANCE );
 for ( int i = 0; i < files.length; i++ ) {
     System.out.println(files[i]);
 }
Frederik
Actually when I try the first solution it tells me that IOFileFilter must implement the inherited abstract method IOFileFilter.accept(File,String).I'm working with List<File> throughout my code so I need to get a List<File>.
Marquinio
Anyone has any tips?
Marquinio
You should extend from AbstractFileFilter. I've changed to code to reflect that.
Frederik
If you have to implement IOFileFilter.accept(File,String), then why dont you just put a call back to accept(File) from it?
Andrew Dyster