문제

I want to collect all (sub-)directories within directory matching a given name using Apache IO Commons. While I can solve this task for files using a NameFileFilter in combination with FileUtils.listFiles, I can't find a solution to do this for folders.

I tried the following snippet:

IOFileFilter fileFilter = new NameFileFilter(fileName);
Collection<File> fileList = FileUtils.listFilesAndDirs(rootFolder, fileFilter, TrueFileFilter.INSTANCE);

It identifies folders and subfolders, but does not filter them according to the NameFileFilter. What am I doing wrong?

도움이 되었습니까?

해결책

Your code only looks for files with this name, not for any directories.

This should work:

IOFileFilter nameFilter = new NameFileFilter(fileName);
Collection<File> fileList = FileUtils.listFilesAndDirs(rootFolder, 
  new NotFileFilter(TrueFileFilter.INSTANCE),
  nameFilter);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top