Java FalseFileFilter-class And Method Code Example


Here is an example of how to use the FalseFileFilter class from the org.apache.commons.io package in Java:

import java.io.File;
import org.apache.commons.io.filefilter.FalseFileFilter;

public class FalseFileFilterExample {
    public static void main(String[] args) {
        // Create a FalseFileFilter
        FalseFileFilter falseFileFilter = FalseFileFilter.FALSE;

        // Use the filter to list all files in the current directory
        File dir = new File(".");
        File[] files = dir.listFiles(falseFileFilter);
        for (File file : files) {
            System.out.println(file.getName());
        }
    }
}

In this example, we create an instance of FalseFileFilter by calling the FalseFileFilter.FALSE static field.

Since FalseFileFilter filters out all the files, the above program will not return any file names, because the listFiles() method will return an empty array when passed this filter.

You can use it to negate other filters. For example, you can use FileFilterUtils.notFileFilter(FileFilter) to return a filter that negates the result of another filter.

import java.io.File;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;

public class FalseFileFilterExample {
    public static void main(String[] args) {
        // Create a filter that negates the result of another filter
        FileFilter notTrueFileFilter = FileFilterUtils.notFileFilter(TrueFileFilter.INSTANCE);

        // Use the filter to list all files in the current directory
        File dir = new File(".");
        File[] files = dir.listFiles(notTrueFileFilter);
        for (File file : files) {
            System.out.println(file.getName());
        }
    }
}

In this example, the TrueFileFilter.INSTANCE would return all files, but the notFileFilter() method negates the result, so the listFiles() method returns an empty array.