Java: 自定义文件过滤器

Java: 自定义文件过滤器

package util;


import java.io.File;


public class CustomFileFilter extends javax.swing.filechooser.FileFilter {

    private String[] extentions;


    public CustomFileFilter(String[] extentions) {

        if (extentions == null) { throw new IllegalArgumentException(

            "No file extentions are given."); }

        this.extentions = extentions;

    }


    public boolean accept(File file) {

        String filename = file.getName();

        boolean result = false;


        for (int i = 0; i < extentions.length; ++i) {

            if (filename.toLowerCase().endsWith(extentions[i].toLowerCase())) {

                result = true;

                break;

            }

        }


        return result || file.isDirectory();

    }


    public String getDescription() {

        String desc = "";

        for (int i = 0; i < extentions.length; ++i) {

            desc += "*" + extentions[i] + " ";

        }


        return desc;

    }

}


你可能感兴趣的:(Java: 自定义文件过滤器)