Java Folder Read by Extension getFilesWithinPath(File path, String fileExtension)

Here you can find the source of getFilesWithinPath(File path, String fileExtension)

Description

Returns all the files within the parent directory (including sub-dirs) that have the supplied extension.

License

Open Source License

Declaration

public static List<File> getFilesWithinPath(File path, String fileExtension) 

Method Source Code


//package com.java2s;
/*/*from   w  w  w.  ja v  a2  s .  c  o  m*/
 * Robonobo Common Utils
 * Copyright (C) 2008 Will Morton (macavity@well.com) & Ray Hilton (ray@wirestorm.net)
    
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
    
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
    
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Returns all the files within the parent directory (including sub-dirs)
     * that have the supplied extension. If extension is null, will return all
     * files. Will not return the sub-dirs themselves.
     * 
     * If the path is a file, it will be returned in the list it matches, or else an empty list will be returned
     */
    public static List<File> getFilesWithinPath(File path, String fileExtension) {
        List<File> result = new ArrayList<File>();
        if (!path.isDirectory()) {
            if (fileExtension == null || fileExtension.equalsIgnoreCase(getFileExtension(path)))
                result.add(path);
            return result;
        }
        addChildFilesToList(path, result, fileExtension);
        return result;
    }

    /**
     * Returns the file extension, without the initial period. If there is no
     * period in the file name, returns an empty string.
     */
    public static String getFileExtension(File f) {
        String fileName = f.getName();
        if (fileName.contains("."))
            return fileName.substring(fileName.lastIndexOf(".") + 1);
        else
            return "";
    }

    private static void addChildFilesToList(File directory, List<File> list, String fileExtension) {
        for (File f : directory.listFiles()) {
            if (f.isDirectory())
                addChildFilesToList(f, list, fileExtension);
            else if (fileExtension == null || fileExtension.equalsIgnoreCase(getFileExtension(f)))
                list.add(f);
        }
    }
}

Related

  1. getFilesForType(final File target, final String extension)
  2. getFilesFromExtension(String directory, String[] extensions)
  3. getFilesWithExtension(File aDirectory, final String aExtension)
  4. getFilesWithExtension(File dir, String extension)
  5. getFilesWithExtension(String directory, String extension)