Java File Name Get getFileNamesRecursive(final String pDataDir, final FilenameFilter pFilter, final int pMaxFiles)

Here you can find the source of getFileNamesRecursive(final String pDataDir, final FilenameFilter pFilter, final int pMaxFiles)

Description

Do a recursive file find.

License

Open Source License

Parameter

Parameter Description
pDataDir The data directory.
pFilter The file filter.
pMaxFiles The maximum number of files.

Return

The file names.

Declaration

public static final String[] getFileNamesRecursive(final String pDataDir, final FilenameFilter pFilter,
        final int pMaxFiles) 

Method Source Code

//package com.java2s;
/**//from   w w  w .ja v a2  s  .com
 * (C) Copyright 2007, Deft Labs.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 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 Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

import java.io.FilenameFilter;

import java.util.LinkedList;

public class Main {
    /**
     * Do a recursive file find.
     * @param pDataDir The data directory.
     * @param pFilter The file filter.
     * @param pMaxFiles The maximum number of files.
     * @return The file names.
     */
    public static final String[] getFileNamesRecursive(final String pDataDir, final FilenameFilter pFilter,
            final int pMaxFiles) {
        final File currentDir = new File(pDataDir);
        // Check to see if it's a directory.
        if (!isDir(currentDir)) {
            throw new IllegalStateException("Not a dir: " + pDataDir);
        }

        final LinkedList<String> files = new LinkedList<String>();
        final File[] listFiles = currentDir.listFiles();

        if (listFiles != null) {
            for (final File listFile : listFiles) {
                if (files.size() == pMaxFiles)
                    break;
                if (listFile.isDirectory()) {
                    final String[] newFiles = getFileNamesRecursive(listFile.getAbsolutePath(), pFilter, pMaxFiles);

                    for (final String newFile : newFiles) {
                        if (files.size() == pMaxFiles)
                            break;
                        files.addLast(newFile);

                    }
                } else {
                    if (pFilter.accept(currentDir, listFile.getName())) {
                        if (files.size() == pMaxFiles)
                            break;
                        files.addLast(listFile.getAbsolutePath());
                    }
                }
            }
        }

        return files.toArray(new String[files.size()]);
    }

    /**
     * Checks to see if the object passed is a directory.
     * @param pFile The file.
     * @return True if the location is a directory.
     * @throws OemException
     */
    public final static boolean isDir(final String pFile) {
        return isDir(new File(pFile));
    }

    /**
     * Checks to see if the object passed is a directory.
     * @param pFile The file.
     * @return True if the location is a directory.
     */
    public final static boolean isDir(final File pFile) {
        return pFile.isDirectory();
    }
}

Related

  1. getFileNames(String path, boolean withFullPath)
  2. getFileNames_STR(String path)
  3. getFileNamesFromDir(File rootDir, String indexName)
  4. getFileNamesInJar(File source, String folder, String extension)
  5. getFileNamesOld(File zipFile)
  6. getFileNameStartingWith(String directory, String prefix)
  7. getFilenameSuffix(final File file)
  8. getFileNameUtil(String dataPath)
  9. getFileNameWithAddedExtension(File parent, File f, String ext)