Java Folder Read getFilesOfDirectory(File directory)

Here you can find the source of getFilesOfDirectory(File directory)

Description

get Files Of Directory

License

Mozilla Public License

Declaration

public static List<File> getFilesOfDirectory(File directory) 

Method Source Code


//package com.java2s;
/* Copyright (C) 2013-2015 BMW Group
 * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
 * Author: Juergen Gehring (juergen.gehring@bmw.de)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import java.io.File;

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

public class Main {
    public static List<File> getFilesOfDirectory(File directory) {
        return getFilesOfDirectory(directory, null);
    }/*ww  w.j  av a 2 s.  c om*/

    public static List<File> getFilesOfDirectory(File directory, String fileExtension) {
        List<File> fileList = new LinkedList<>();

        if (!directory.isDirectory()) {
            return fileList;
        }

        for (File fileInDirectory : directory.listFiles()) {
            if (fileInDirectory.isDirectory()) {
                fileList.addAll(getFilesOfDirectory(fileInDirectory, fileExtension));
            } else if (fileInDirectory.isFile()
                    && (null == fileExtension || fileInDirectory.getName().endsWith("." + fileExtension))) {
                fileList.add(fileInDirectory);
            }
        }

        Collections.sort(fileList, new java.util.Comparator<File>() {
            @Override
            public int compare(File file1, File file2) {
                return file1.getAbsolutePath().compareTo(file2.getAbsolutePath());
            }
        });

        return fileList;
    }
}

Related

  1. getFilesInFolder(final String pathToFolder, final String suffix)
  2. getFilesInFolderByRegex(File folder, final String regex)
  3. getFilesMatchingRegexp(final File directory, final String regexp)
  4. getFilesModDate(String path)
  5. getFilesOf(File dir)
  6. getFilesOfTypeInDirectory(File directory, String filetype)
  7. getFilesRegex(final File root, final String[] regex)
  8. getFilesStartingWith(File parentDir, String prefix)
  9. getFilesStartingWith(String dirName, String startsWith)