Java File List Load getFileListingNoSort(File aStartingDir, boolean recursive)

Here you can find the source of getFileListingNoSort(File aStartingDir, boolean recursive)

Description

Get a list of all files/directories in a directory structure.

License

Open Source License

Parameter

Parameter Description
aStartingDir a parameter
recursive a parameter

Return

List

Declaration

private static List<File> getFileListingNoSort(File aStartingDir, boolean recursive) 

Method Source Code


//package com.java2s;
/*/*  w w  w  .  ja va  2s . c om*/
Copyright (C) 2010-2011  Tim Telcik <telcik@gmail.com>
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

import java.util.List;

public class Main {
    /**
     * Get a list of all files/directories in a directory structure. 
     * Can optionally recurse into any number of nested directories. 
     * 
     * @param aStartingDir
     * @param recursive
     * @return List<File>
     */
    private static List<File> getFileListingNoSort(File aStartingDir, boolean recursive) {
        List<File> result = new ArrayList<File>();
        File[] filesAndDirs = aStartingDir.listFiles();
        List<File> filesDirs = Arrays.asList(filesAndDirs);
        for (File file : filesDirs) {
            result.add(file); // always add, even if directory
            if (file.isDirectory() && recursive) {
                List<File> nestedList = getFileListingNoSort(file, recursive);
                result.addAll(nestedList);
            }
        }
        return result;
    }
}

Related

  1. getFileListing(File dir)
  2. getFileListing(File directory)
  3. getFileListing(File file)
  4. getFileListing(File file, String... extension)
  5. getFileListingLogs(File aStartingDir)
  6. getFilesByExt(String dirName, String ext)
  7. getFilesFromDir(File dir)
  8. getFilesFromDirectory(final File folder)
  9. getFilesFromDirectory(final String directory)