Here you can find the source of getFiles(File folder, String... extensions)
Parameter | Description |
---|---|
folder | a parameter |
extensions | a parameter |
public static File[] getFiles(File folder, String... extensions)
//package com.java2s; /* Copyright 2009 Jeremy Chone - Licensed under the Apache License, Version 2.0 * http://www.apache.org/licenses/LICENSE-2.0 *//*from w w w .j a v a 2 s . c o m*/ import java.io.File; import java.io.FileFilter; import java.util.Arrays; import java.util.Comparator; import java.util.HashSet; import java.util.Set; public class Main { /** * Return the list of Files (excluding folders) for this Folder for the given extensions. <br /> * Note 1: The list will be ordered by name via Java to make it consistent across file systems. <br /> * Note 2: The extensions are case-insensitive. * * @param folder * @param extensions */ public static File[] getFiles(File folder, String... extensions) { final Set<String> exts = new HashSet<String>(); for (String ext : extensions) { exts.add(ext.toLowerCase()); } File[] files = folder.listFiles(new FileFilter() { @Override public boolean accept(File file) { if (file.isDirectory()) { return false; } String name = file.getName(); String ext = getFileNameAndExtension(name)[1]; return exts.contains(ext.toLowerCase()); } }); Arrays.sort(files, new Comparator<File>() { @Override public int compare(File f1, File f2) { // TODO Auto-generated method stub return f1.getName().compareTo(f2.getName()); } }); return files; } /** * Return a String[2] with the first element being the file path/name * (before the last '.'), and the second element as the extension (from the * last '.' on). If no extension, return an empty string. <br /> * <br /> * * Note: The extension is found by using lastIndexOf('.') * * @param fullFileName * @return */ public static String[] getFileNameAndExtension(String fullFileName) { String[] fileNameAndExtension = new String[2]; int lastDotIdx = fullFileName.lastIndexOf('.'); if (lastDotIdx != -1) { fileNameAndExtension[0] = fullFileName.substring(0, lastDotIdx); fileNameAndExtension[1] = fullFileName.substring(lastDotIdx); } else { fileNameAndExtension[0] = fullFileName; fileNameAndExtension[1] = ""; } return fileNameAndExtension; } }