What is the advantage of FileFilter / FilenameFilter

Working with a small Java tool to get a set of files based on a set of file name extensions, I discuss between using listFiles () and using continue;when I encounter a bad file, as well as using a custom FileFilter or FilenameFilter to do the same for me.

It seems to me that these methods are convenient methods for integrated tools, such as viewing Swing files, and are not more effective than the manual method if we are not connected to any of these tools. It's right? Are there any other benefits to these filters?

+3
source share
4 answers

JDK 1.6:

public String[] list(FilenameFilter filter) {
    String names[] = list();
    if ((names == null) || (filter == null)) {
        return names;
    }
    ArrayList v = new ArrayList();
    for (int i = 0 ; i < names.length ; i++) {
        if (filter.accept(this, names[i])) {
            v.add(names[i]);
        }
    }
    return (String[])(v.toArray(new String[v.size()]));
}

, , , , . , , - , , :)

+2

, , ( - , , , ). , , .

+1

No, think that everything is fine with your money. These are convenient methods. They simply take the name (and know the extension that you installed initially) and check to see if it should be included in the list. In many ways, this is the same as you.

From the Java documentation:

public boolean accept(File dir, String name)

Checks if the specified file should be included in the file list.

This is exactly what you are doing.

0
source

Source: https://habr.com/ru/post/1780409/


All Articles