Java Directory to File List getAllFileNames(File basedir, String path)

Here you can find the source of getAllFileNames(File basedir, String path)

Description

The method return a list of all file names under the given directory and subdirectories of itself.

License

Open Source License

Parameter

Parameter Description
basedir the File instance of the basic directory
path the part of directory path

Return

an ArrayList with file names as pathes

Declaration

public static ArrayList<String> getAllFileNames(File basedir,
        String path) 

Method Source Code

//package com.java2s;
/*//from  www.j  a  va  2 s  . c  o m
 * 
 * $Revision$ $Date$
 * 
 * This file is part of *** M y C o R e *** See http://www.mycore.de/ for
 * details.
 * 
 * This program is free software; you can use it, redistribute it and / or
 * modify it under the terms of the GNU General Public License (GPL) as
 * published by the Free Software Foundation; either version 2 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, in a file called gpl.txt or license.txt. If not, write to the
 * Free Software Foundation Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307 USA
 */

import java.io.File;

import java.util.ArrayList;

public class Main {
    private static String SLASH = System.getProperty("file.separator");

    /**
     * The method return a list of all file names under the given directory and subdirectories of itself.
     * 
     * @param basedir
     *            the File instance of the basic directory
     * @return an ArrayList with file names as pathes
     */
    public static ArrayList<String> getAllFileNames(File basedir) {
        ArrayList<String> out = new ArrayList<String>();
        File[] stage = basedir.listFiles();

        for (File element : stage) {
            if (element.isFile()) {
                out.add(element.getName());
            }

            if (element.isDirectory()) {
                out.addAll(getAllFileNames(element, element.getName()
                        + SLASH));
            }
        }

        return out;
    }

    /**
     * The method return a list of all file names under the given directory and subdirectories of itself.
     * 
     * @param basedir
     *            the File instance of the basic directory
     * @param path
     *            the part of directory path
     * @return an ArrayList with file names as pathes
     */
    public static ArrayList<String> getAllFileNames(File basedir,
            String path) {
        ArrayList<String> out = new ArrayList<String>();
        File[] stage = basedir.listFiles();

        for (File element : stage) {
            if (element.isFile()) {
                out.add(path + element.getName());
            }

            if (element.isDirectory()) {
                out.addAll(getAllFileNames(element,
                        path + element.getName() + SLASH));
            }
        }

        return out;
    }
}

Related

  1. getAllFile2(String Url)
  2. getAllFileFromPath(File file, List fileList, String ed)
  3. getAllFileName(String path)
  4. GetAllFileName(String path)
  5. getAllFileName(String path, ArrayList fileName)
  6. getAllFileNames(String path, String suffix, boolean isDepth)
  7. getAllFileNamesByPath(String path)
  8. getAllFileNamesInDir(String folderName)
  9. getAllFileNamesWithExtension(String directory, final String extension)