Java Folder Read getFiles(File directory)

Here you can find the source of getFiles(File directory)

Description

Get a listing of all the files in this directory which matches the given pattern.

License

Open Source License

Parameter

Parameter Description
directory the directory to scan to start

Return

a list of file references to discovered files matching the given name pattern.

Declaration

public static List<File> getFiles(File directory) 

Method Source Code

//package com.java2s;
/*//from  www .  j  a  v a 2s. com
 * Copyright (c) 2006 Stephan D. Cote' - All rights reserved.
 * 
 * This program and the accompanying materials are made available under the 
 * terms of the MIT License which accompanies this distribution, and is 
 * available at http://creativecommons.org/licenses/MIT/
 *
 * Contributors:
 *   Stephan D. Cote 
 *      - Initial API and implementation
 */

import java.io.File;

import java.util.ArrayList;

import java.util.List;

import java.util.regex.Pattern;

public class Main {
    /**
     * Get a listing of all the files in this directory which matches the given 
     * pattern.
     * 
     * <p>All the files in the directory will be returned.</p>
     * 
     * <p>Only files are returned; directories are not included in the list.</p>
     * 
     * @param directory the directory to scan to start 
     * 
     * @return a list of file references to discovered files matching the given name pattern.
     */
    public static List<File> getFiles(File directory) {
        return getFiles(directory, null, false);
    }

    /**
     * Get a listing of all the files in this directory which matches the given 
     * pattern.
     * 
     * <p>All the files in the directory will be returned.</p>
     * 
     * <p>Only files are returned; directories are not included in the list.</p>
     * 
     * @param directory the directory to scan to start 
     * @param recurse true to include all sub-directories as well
     * 
     * @return a list of file references to discovered files matching the given name pattern.
     */
    public static List<File> getFiles(File directory, boolean recurse) {
        return getFiles(directory, null, recurse);
    }

    /**
     * Get a listing of all the files in this directory which matches the given 
     * pattern.
     * 
     * <p>The pattern is a regular expression (regex) which is applied to the 
     * entire file path of the discovered file. If the path of the file matches, 
     * the file is placed in the list of return values.</p>
     * 
     * <p>Only files are returned; directories are not included in the list.</p>
     * 
     * @param directory the directory to scan to start 
     * @param pattern the regex pattern to match, if null, no files will be excluded
     * 
     * @return a list of file references to discovered files matching the given name pattern.
     */
    public static List<File> getFiles(File directory, String pattern) {
        return getFiles(directory, pattern, false);
    }

    /**
     * Get a listing of all the files in this directory which matches the given 
     * pattern.
     * 
     * <p>The pattern is a regular expression (regex) which is applied to the 
     * entire file path of the discovered file. If the path of the file matches, 
     * the file is placed in the list of return values.</p>
     * 
     * <p>Only files are returned; directories are not included in the list.</p>
     * 
     * @param directory the directory to scan to start 
     * @param pattern the regex pattern to match, if null, no files will be excluded
     * @param recurse true to include all sub-directories as well
     * 
     * @return a list of file references to discovered files matching the given name pattern.
     */
    public static List<File> getFiles(File directory, String pattern, boolean recurse) {
        final List<File> list = new ArrayList<File>();

        Pattern regex = null;

        if (pattern != null && pattern.trim().length() > 0) {
            regex = Pattern.compile(pattern);
        }

        File[] listOfFiles = directory.listFiles();
        if (listOfFiles != null) {

            for (int i = 0; i < listOfFiles.length; i++) {
                if (listOfFiles[i].isFile()) {
                    // This is where we do pattern checks on the entire file path
                    if (regex != null) {
                        if (regex.matcher(listOfFiles[i].getAbsolutePath()).matches()) {
                            list.add(listOfFiles[i]);
                        }
                    } else {
                        list.add(listOfFiles[i]);
                    }
                } else if (listOfFiles[i].isDirectory()) {
                    if (recurse) {
                        list.addAll(getFiles(listOfFiles[i], pattern, recurse));
                    }
                } // isFile || dir
            } // for
        } // !null

        return list;
    }
}

Related

  1. getFiles(File dir, File... excludes)
  2. getFiles(File dir, final String pattern)
  3. getFiles(File dir, String... names)
  4. getFiles(File directory)
  5. getFiles(File directory)
  6. getFiles(File directory)
  7. getFiles(File f, List response)
  8. getFiles(File file)
  9. getFiles(File file, Collection ret)