Java Folder Read getFilesOfTypeInDirectory(File directory, String filetype)

Here you can find the source of getFilesOfTypeInDirectory(File directory, String filetype)

Description

Returns an array of files of the specified type in the specified directory.

License

Open Source License

Parameter

Parameter Description
directory Target directory
filetype Target filetype

Return

array of files of the specified type in the specified directory

Declaration

public static File[] getFilesOfTypeInDirectory(File directory, String filetype) 

Method Source Code


//package com.java2s;
/*/*from   w  w w. j a v a2 s  .c om*/
 * ReflectionHelper
 *
 * 8/24/13
 *
 * Copyright (c) 2013, Marcus Stojcevich
 * All rights reserved.
 * See the included LICENSE file
 */

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Returns an array of files of the specified type in the specified
     * directory.
     * @param directory Target directory
     * @param filetype Target filetype
     * @return array of files of the specified type in the specified
     * directory
     */
    public static File[] getFilesOfTypeInDirectory(File directory, String filetype) {
        List<File> fileList = new ArrayList<File>();

        for (String filename : directory.list()) {
            if (filename.toLowerCase().endsWith("." + filetype.trim().toLowerCase())) {
                fileList.add(new File(directory.getAbsolutePath() + File.separator + filename));
            }
        }

        return fileList.toArray(new File[fileList.size()]);
    }
}

Related

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