Java File Name Get getFileNames(final String pDataDir, final String pFilter)

Here you can find the source of getFileNames(final String pDataDir, final String pFilter)

Description

Returns files from the data dir based on the max allowed.

License

Open Source License

Return

The file names.

Declaration

public static final String[] getFileNames(final String pDataDir, final String pFilter) 

Method Source Code

//package com.java2s;
/**/* w w w .  ja va  2 s . c om*/
 * (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 {
    public static final int DEFAULT_MAX_FILE_NAMES = 10000;

    /**
     * Returns files from the data dir based on the max allowed.
     * @return The file names.
     */
    public static final String[] getFileNames(final String pDataDir, final String pFilter) {
        return getFileNames(pDataDir, pFilter, false);
    }

    /**
     * Returns files from the data dir based on the max allowed.
     * @return The file names.
     */
    public static final String[] getFileNames(final String pDataDir, final String pFilter, final boolean pExclude) {
        return getFileNames(pDataDir, pFilter, pExclude, false);
    }

    /**
     * Returns files from the data dir based on the max allowed.
     * @param pDataDir The data directory.
     * @param pExclude Set to true to get all the files but this filter.
     * @param pEndsWith Set to true to indicate that the filter be the end
     * value.
     * @return The files names.
     */
    public static final String[] getFileNames(final String pDataDir, final String pFilter, final boolean pExclude,
            final boolean pEndsWith) {
        return getFileNames(pDataDir, pFilter, pExclude, pEndsWith, false);
    }

    /**
     * Returns files from the data dir based on the max allowed.
     * @param pDataDir The data directory.
     * @param pExclude Set to true to get all the files but this filter.
     * @param pEndsWith Set to true to indicate that the filter be the end
     * value.
     * @param pRecursive Set to true to walk the directories below. Uses the
     * DEFAULT_MAX_FILE_NAMES (10,000).
     * @return The files names.
     */
    public static final String[] getFileNames(final String pDataDir, final String pFilter, final boolean pExclude,
            final boolean pEndsWith, final boolean pRecursive) {
        return getFileNames(pDataDir, pFilter, pExclude, pEndsWith, false, DEFAULT_MAX_FILE_NAMES);
    }

    /**
     * Returns files from the data dir based on the max allowed.
     * @param pDataDir The data directory.
     * @param pExclude Set to true to get all the files but this filter.
     * @param pEndsWith Set to true to indicate that the filter be the end
     * value.
     * @param pRecursive Set to true to walk the directoeis below.
     * @param pMaxFiles The maximum number of files to return. This only
     * applies if to recursive is set to true.
     * @return The files names.
     */
    public static final String[] getFileNames(final String pDataDir, final String pFilter, final boolean pExclude,
            final boolean pEndsWith, final boolean pRecursive, final int pMaxFiles) {
        final File dataDir = new File(pDataDir);
        if (!dataDir.exists()) {
            throw new IllegalStateException("invalid dir: " + pDataDir);
        }

        final FilenameFilter filter = getFilenameFilter(pFilter, pExclude, pEndsWith);

        if (!pRecursive)
            return dataDir.list(filter);

        return getFileNamesRecursive(pDataDir, filter, pMaxFiles);
    }

    /**
     * Returns the file filter object. This checks the cache based on the
     * filter value.
     * @param pFilter The filter.
     * @param pExclude The exclude flag.
     */
    public static final FilenameFilter getFilenameFilter(final String pFilter, final boolean pExclude) {
        return getFilenameFilter(pFilter, pExclude, false);
    }

    /**
     * Returns the file filter object. This checks the cache based on the
     * filter value. This method can be refactored :-) In a hurry now,.
     * @param pFilter The filter.
     * @param pExclude The exclude flag.
     * @param pEndsWith  Make sure the suffix is there (in the filter).
     */
    public static final FilenameFilter getFilenameFilter(final String pFilter, final boolean pExclude,
            final boolean pEndsWith) {
        FilenameFilter filter;
        if (pExclude) {

            if (pEndsWith) {
                filter = new FilenameFilter() {
                    public boolean accept(final File pFile, final String pName) {
                        return !pName.endsWith(pFilter);
                    }
                };
            } else {
                filter = new FilenameFilter() {
                    public boolean accept(final File pFile, final String pName) {
                        return (pName.indexOf(pFilter) == -1) ? true : false;
                    }
                };
            }
        } else {
            if (pEndsWith) {
                filter = new FilenameFilter() {
                    public boolean accept(final File pFile, final String pName) {
                        return pName.endsWith(pFilter);
                    }
                };
            } else {
                filter = new FilenameFilter() {
                    public boolean accept(final File pFile, final String pName) {
                        return (pName.indexOf(pFilter) != -1) ? true : false;
                    }
                };
            }
        }

        return filter;
    }

    /**
     * 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(File path, Vector result)
  2. getFileNames(File zipFile, Pattern pattern)
  3. getFileNames(File[] files)
  4. getFilenames(File[] files)
  5. getFileNames(final String path, final String regex)
  6. getFilenames(String dir, FilenameFilter filter)
  7. getFilenames(String dirctory)
  8. getFileNames(String directory, String file)
  9. getFileNames(String dirPath)