Java Directory to File List getAllFilesInDir(boolean traverseSubDirs, File dir, boolean includeHidden, String prefix)

Here you can find the source of getAllFilesInDir(boolean traverseSubDirs, File dir, boolean includeHidden, String prefix)

Description

get All Files In Dir

License

Open Source License

Declaration

private static List<String> getAllFilesInDir(boolean traverseSubDirs, File dir, boolean includeHidden,
            String prefix) 

Method Source Code

//package com.java2s;
/*  MonkeyTalk - a cross-platform functional testing tool
Copyright (C) 2012 Gorilla Logic, Inc.//from   www. ja v a  2  s. co m
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.
    
You should have received a copy of the GNU Affero 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.List;

public class Main {
    private static List<String> getAllFilesInDir(boolean traverseSubDirs, File dir, boolean includeHidden,
            String prefix) {
        List<String> files = new ArrayList<String>();
        String fileNames[] = dir.list();

        for (String fileName : fileNames) {
            File currentFile = new File(dir, fileName);
            if (currentFile.isHidden() && !includeHidden) {
                continue;
            }
            if (currentFile.isDirectory()) {
                if (traverseSubDirs) {
                    String pfx;
                    if (prefix == null || prefix.length() == 0) {
                        pfx = fileName;
                    } else {
                        pfx = prefix + "/" + fileName;
                    }
                    files.addAll(getAllFilesInDir(true, currentFile, includeHidden, pfx));
                }
            } else {
                String fullname = fileName;
                if (prefix != null && prefix.length() > 0) {
                    fullname = prefix + "/" + fileName;
                }
                files.add(fullname);
            }
        }
        return files;
    }
}

Related

  1. getAllFilesFromDir(File dir)
  2. getAllFilesFromFolder(File aFolder, FilenameFilter filenameFilter)
  3. getAllFilesFromFolder(File sampleFolder, ArrayList fileList, FilenameFilter filenameFilter)
  4. getAllFilesIn(File dir, final String type)
  5. getAllFilesInAllSubDirectories(File parentDir, FileFilter filter)
  6. getAllFilesInDirectory(File dir)
  7. getAllFilesInDirectory(File directory, List files)
  8. getAllFilesInDirectory(String directoryPath)
  9. getAllFilesInDirectory(String dirName)