Java Directory to File List getAllFiles(String[] args, boolean recurse)

Here you can find the source of getAllFiles(String[] args, boolean recurse)

Description

Get all files from the given location(s).

License

Open Source License

Parameter

Parameter Description
args array with file and directory names.
recurse recurse into all directories

Return

a list with all given files and all files in the given directories.

Declaration

public static List<File> getAllFiles(String[] args, boolean recurse) 

Method Source Code


//package com.java2s;
/*----------------------------------------------------------------------------
 This file is part of deegree, http://deegree.org/
 Copyright (C) 2001-2009 by://www. j  a  v a 2  s  .co  m
   Department of Geography, University of Bonn
 and
   lat/lon GmbH
    
 This library 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 2.1 of the License, or (at your option)
 any later version.
 This library 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 Lesser General Public License for more
 details.
 You should have received a copy of the GNU Lesser General Public License
 along with this library; if not, write to the Free Software Foundation, Inc.,
 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    
 Contact information:
    
 lat/lon GmbH
 Aennchenstr. 19, 53177 Bonn
 Germany
 http://lat-lon.de/
    
 Department of Geography, University of Bonn
 Prof. Dr. Klaus Greve
 Postfach 1147, 53001 Bonn
 Germany
 http://www.geographie.uni-bonn.de/deegree/
    
 e-mail: info@deegree.org
----------------------------------------------------------------------------*/

import java.io.File;
import java.io.FileFilter;

import java.util.LinkedList;
import java.util.List;

public class Main {
    /**
     * Get all files from the given location(s).
     *
     * @param args
     *            array with file and directory names.
     * @param recurse recurse into all directories
     * @param fileExt only get files with the given file suffix
     * @return a list with all given files and all files in the given directories.
     */
    public static List<File> getAllFiles(String[] args, boolean recurse, final String fileExt) {
        List<File> result = new LinkedList<File>();
        for (String filename : args) {
            result.addAll(getFiles(new File(filename), recurse, new FileFilter() {
                public boolean accept(File pathname) {
                    return pathname.getName().endsWith(fileExt);
                }
            }));
        }
        return result;
    }

    /**
     * Get all files from the given location(s).
     *
     * @param args
     *            array with file and directory names.
     * @param recurse recurse into all directories
     * @return a list with all given files and all files in the given directories.
     */
    public static List<File> getAllFiles(String[] args, boolean recurse) {
        return getAllFiles(args, recurse, "");
    }

    private static List<File> getFiles(File root, boolean recurse, FileFilter fileFilter) {
        List<File> result = new LinkedList<File>();
        if (root.isDirectory()) {
            for (File file : root.listFiles()) {
                if (file.isDirectory() && recurse) {
                    result.addAll(getFiles(file, recurse, fileFilter));
                } else if (file.isFile() && fileFilter.accept(file)) {
                    result.add(file);
                }
            }
        } else {
            result.add(root);
        }
        return result;
    }
}

Related

  1. getAllFiles(String folderPath)
  2. getAllFiles(String path)
  3. getAllFiles(String path, boolean isDepth)
  4. getAllFiles(String sDirectoryPath)
  5. getAllFiles(String zipName, String fileNameRegEx)
  6. getAllFilesByExtension(String path, String extension)
  7. getAllFilesByProfix(String path, String suffix, List files)
  8. getAllFilesEndingWith(String path, final String extension)
  9. getAllFilesEndingWith(String path, final String extension)