Java Folder Read by Extension getFiles(File folder, String... extensions)

Here you can find the source of getFiles(File folder, String... extensions)

Description

Return the list of Files (excluding folders) for this Folder for the given extensions.

License

Apache License

Parameter

Parameter Description
folder a parameter
extensions a parameter

Declaration

public static File[] getFiles(File folder, String... extensions) 

Method Source Code

//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;
    }
}

Related

  1. getFiles(File baseDir, String validExtension, List files)
  2. getFiles(File dir, final String extension)
  3. getFiles(File dir, final String extension)
  4. getFiles(File dir, final String extensionFilter)
  5. getFiles(File dir, String fileExtension)
  6. getFiles(final String path, final String ext)
  7. getFiles(String dir, String ext)
  8. getFiles(String dir, String... extension)
  9. getFiles(String directory, String ext)