Java Directory to File List getAllFiles(File inFileOrDir, boolean recurseDir)

Here you can find the source of getAllFiles(File inFileOrDir, boolean recurseDir)

Description

get All Files

License

Open Source License

Declaration

public static List<File> getAllFiles(File inFileOrDir,
            boolean recurseDir) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (c) 2015 IBM Corporation.//from  w  ww.  ja va2  s  .  c  om
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *****************************************************************************/

import java.io.File;

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

import java.util.Stack;

public class Main {
    public static List<File> getAllFiles(File inFileOrDir,
            boolean recurseDir) {
        final List<File> ret = new LinkedList<File>();
        Stack<File> stack = new Stack<File>();
        stack.add(inFileOrDir);
        while (!stack.isEmpty()) {
            File f = stack.pop();
            if (f.isFile()) {
                ret.add(f);
            } else {
                assert f.isDirectory() : f;
                if (recurseDir) {
                    for (File cf : f.listFiles()) {
                        stack.add(cf);
                    }
                }
            }
        }
        return ret;
    }
}

Related

  1. getAllFiles(File dir, Pattern filter)
  2. getAllFiles(File directory)
  3. getAllFiles(File directory)
  4. getAllFiles(File directory, boolean hidden)
  5. getAllFiles(File directory, String rootPath)
  6. getAllFiles(File input)
  7. getAllFiles(File outputDir, List files)
  8. getAllFiles(File path, List fileList, boolean recursive)
  9. getAllFiles(File root, FileFilter fileFilter)