0

i have used Apache FileUtils and IOFileFilter to list all files under a folder recursively excluding .svn folders. Here is the code i tried

File selectedFolder = new File(path);\\path to folder to list
        final IOFileFilter dirs = new IOFileFilter() {
            @Override
            public boolean accept(File file, String s) {
                return file.isDirectory();
            }

            @Override
            public boolean accept(File file) {
                // TODO Auto-generated method stub
                if(file.getName().toLowerCase().equalsIgnoreCase(".svn")||file.getName().toLowerCase().contains(".svn"))

                return false;
                else return true;
            }



        };
        filesList.addAll(FileUtils.listFiles(selectedFolder,dirs, TrueFileFilter.INSTANCE));

I am getting the error

java.lang.IllegalArgumentException: Parameter 'directory' is not a directory
    at org.apache.commons.io.FileUtils.validateListFilesParameters(FileUtils.java:545)
    at org.apache.commons.io.FileUtils.listFiles(FileUtils.java:521)

Can anyone tell me where am going wrong. I feel there is something wrong with the filter used. I could not figure it out

VamsiKrishna
  • 751
  • 6
  • 14
  • 29

2 Answers2

3

Actually, FileFilterUtils contains a method called makeSVNAware that you could use. It returns a filter that ignores SVN directories. For example:

filesList.addAll(
    FileUtils.listFiles(selectedFolder, TrueFileFilter.TRUE,
            FileFilterUtils.makeSVNAware(null)));

Note that listFiles expects a file filter as its 2nd argument, and a dir filter as its 3rd. In your code they're the other way round. So if you wouldn't want to use makeSVNAware, your code would look something like this:

File selectedFolder = new File(path); // path to folder to list

final IOFileFilter dirs = new IOFileFilter() {
    @Override
    public boolean accept(File file, String s) {
        return file.isDirectory();
    }

    @Override
    public boolean accept(File file) {
        return (!file.getName().toLowerCase().equalsIgnoreCase(".svn"));
    }

};
// 2nd argument: TRUE filter, returning all files
// 3rd argument: dirs filter, returning all directories except those named .svn
filesList.addAll(FileUtils.listFiles(selectedFolder, TrueFileFilter.TRUE, dirs));
Danny Lagrouw
  • 418
  • 3
  • 7
0

It looks like you have split the functionality into two functions.

The second one should also check for isDirectory() and the first one should also check the name.

Dahaka
  • 507
  • 1
  • 4
  • 14