match File By Extension - Java java.io

Java examples for java.io:File Extension Name

Description

match File By Extension

Demo Code

/**// w  w w. j a va2s  .  c o  m
 *
 * Licensed under the GNU General Public License (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.gnu.org/licenses/gpl.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * 
 * @author Vadim Kisen
 *
 * copyright 2010 by uTest 
 */
//package com.java2s;

import java.io.File;

import java.util.List;

public class Main {
    public static void main(String[] argv) throws Exception {
        File file = new File("Main.java");
        List exts = java.util.Arrays.asList("asdf", "java2s.com");
        System.out.println(matchByExtension(file, exts));
    }

    private static boolean matchByExtension(final File file,
            final List<String> exts) {
        if (file.isDirectory()) {
            return false;
        }

        final String ext = file.getName().substring(
                file.getName().lastIndexOf(".") + 1);
        for (final String extension : exts) {
            if (extension.equalsIgnoreCase(ext)) {
                return true;
            }
        }
        return false;

    }
}

Related Tutorials