Java Folder Read getFiles(File directory)

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

Description

Get a list of all files in a directory, ignoring subdirectories.

License

Apache License

Parameter

Parameter Description
directory The directory for which to get the files.

Return

ArrayList A list of File objects. null if directory does not exist, or is not a directory.

Declaration

public static final ArrayList<File> getFiles(File directory) 

Method Source Code

//package com.java2s;
/*/*  w ww.  j  av a2  s.co m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.io.File;

import java.util.ArrayList;

public class Main {
    /**
     * Get a list of all files in a directory. Optionally, get files in subdirectories as well.
     * 
     * @param directory
     *          The directory for which to get the files.
     * @param getRecursive
     *          Should we get files from subdirectories?
     * @return ArrayList A list of <code>File</code> objects. <code>null</code> if
     *         <code>directory</code> does not exist, or is not a directory.
     */
    public static final ArrayList<File> getFiles(File directory, boolean getRecursive) {
        if (!directory.exists() || !directory.isDirectory()) {
            return null;
        }
        ArrayList<File> fileList = new ArrayList<File>();
        for (File file : directory.listFiles()) {
            if (file.isDirectory()) {
                if (getRecursive) {
                    fileList.addAll(getFiles(file, getRecursive));
                }
            } else {
                fileList.add(file);
            }
        }
        return fileList;
    }

    /**
     * Get a list of all files in a directory, ignoring subdirectories.
     * 
     * @param directory
     *          The directory for which to get the files.
     * @return ArrayList A list of <code>File</code> objects. <code>null</code> if
     *         <code>directory</code> does not exist, or is not a directory.
     */
    public static final ArrayList<File> getFiles(File directory) {
        return getFiles(directory, false);
    }
}

Related

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