开发者

Problem reading subdirectories using FileUtils.listfiles

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(...)?


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]);
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜