Java Directory to File List getAllFilesFromDir(File dir)

Here you can find the source of getAllFilesFromDir(File dir)

Description

Gets the all files from directory.

License

Apache License

Parameter

Parameter Description
dir the dir

Return

A list with all files from the given directory.

Declaration

public static List<File> getAllFilesFromDir(File dir) 

Method Source Code

//package com.java2s;
/**//from   w ww  .ja va2s. c  om
 * Copyright (C) 2007 Asterios Raptis
 *
 * Licensed 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;

import java.util.List;

public class Main {
    /**
     * Gets the all files from directory.
     *
     * @param dir
     *            the dir
     * @return A list with all files from the given directory.
     */
    public static List<File> getAllFilesFromDir(File dir) {
        final List<File> foundedFileList = new ArrayList<>();
        final File[] children = dir.getAbsoluteFile().listFiles();
        if (children == null || children.length < 1) {
            return foundedFileList;
        }
        for (File child : children) {
            // if the entry is not a directory
            if (!child.isDirectory()) {
                // then
                // entry is a file
                foundedFileList.add(child.getAbsoluteFile());
            }
        }
        return foundedFileList;
    }
}

Related

  1. getAllFilesByExtension(String path, String extension)
  2. getAllFilesByProfix(String path, String suffix, List files)
  3. getAllFilesEndingWith(String path, final String extension)
  4. getAllFilesEndingWith(String path, final String extension)
  5. getAllFilesForType(File dir, FilenameFilter filter)
  6. getAllFilesFromFolder(File aFolder, FilenameFilter filenameFilter)
  7. getAllFilesFromFolder(File sampleFolder, ArrayList fileList, FilenameFilter filenameFilter)
  8. getAllFilesIn(File dir, final String type)
  9. getAllFilesInAllSubDirectories(File parentDir, FileFilter filter)