Java examples for java.io:File Extension Name
get Files Filtered By Extension
/**/* w w w .java2s .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.ArrayList; import java.util.List; public class Main { public static void main(String[] argv) throws Exception { File parent = new File("Main.java"); List extensions = java.util.Arrays.asList("asdf", "java2s.com"); System.out.println(getFilesFilteredByExtension(parent, extensions)); } public static List<File> getFilesFilteredByExtension(final File parent, final List<String> extensions) { final List<File> res = new ArrayList<File>(); if (!parent.isDirectory()) { return res; } for (final File file : parent.listFiles()) { if (matchByExtension(file, extensions)) { res.add(file); } } return res; } 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; } }