Java Folder Read getFilesForDirectory(File dir)

Here you can find the source of getFilesForDirectory(File dir)

Description

Method to return the files below the specified directory.

License

Open Source License

Parameter

Parameter Description
dir The directory

Return

The files

Declaration

public static Collection<File> getFilesForDirectory(File dir) 

Method Source Code

//package com.java2s;
/**********************************************************************
Copyright (c) 2004 Andy Jefferson and others. All rights reserved. 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at//from   w  w w .j  a va 2 s .  c  om
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. 
    
    
Contributors:
...
**********************************************************************/

import java.io.File;

import java.util.Collection;

import java.util.HashSet;

public class Main {
    /**
     * Method to return the files below the specified directory.
     * @param dir The directory
     * @return The files
     */
    public static Collection<File> getFilesForDirectory(File dir) {
        if (dir == null) {
            return null;
        }

        Collection files = new HashSet();
        File[] dirFiles = dir.listFiles();
        if (dirFiles != null) {
            for (int i = 0; i < dirFiles.length; i++) {
                if (dirFiles[i].isFile()) {
                    files.add(dirFiles[i]);
                } else {
                    // Check for files in subdirectories
                    Collection childFiles = getFilesForDirectory(dirFiles[i]);
                    if (childFiles != null && childFiles.size() > 0) {
                        files.addAll(childFiles);
                    }
                }
            }
        }

        return files;
    }
}

Related

  1. getFilesComposingPath(File aFile)
  2. getFilesCount(File archiveFile)
  3. getFileset(File projectFolder)
  4. getFilesFolder(String path)
  5. getFilesFor(List patientSets, File inputDirectory)
  6. getFilesForFolder(File folder)
  7. getFilesForPattern(String pathToScan, String startWith, String endsWith)
  8. getFilesFromClassPath(String classpath)
  9. getFilesFromDiskWorker(File folderToLoad, final String searchPattern, final boolean justFolders)