Java JFileChooser retrieveFilesFromDir(File basedir, FileFilter filter)

Here you can find the source of retrieveFilesFromDir(File basedir, FileFilter filter)

Description

retrieve Files From Dir

License

Open Source License

Declaration

public static List<File> retrieveFilesFromDir(File basedir, FileFilter filter) 

Method Source Code


//package com.java2s;
/*/*from  www .j  a v  a  2s . c  o  m*/
 * #%L
 * Katsu Commons Utils
 * %%
 * Copyright (C) 2013 Katsu
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */

import java.io.File;

import java.io.FilenameFilter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

public class Main {
    /**
     * Obtains All files from a directory
     * @param basedir the base directory where files are returned
     * @param filter only files than fullfills filter must be returned (default all files)
     * @param recursive if true subdirs will be inspected for new files (default true)
     * @param sorted if true the returned list will be sorted by absolute paths (default false)
     * @return the files that fullfill the file filter.
     */
    public static List<File> retrieveFilesFromDir(File basedir, final FileFilter filter, boolean recursive,
            boolean sorted) {
        List<File> arrayFiles = null;
        if (basedir.isDirectory() && basedir.canRead()) {
            FilenameFilter fileFilter = new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return filter.accept(new File(dir, name));
                }
            };
            File[] filteredFiles = basedir.listFiles(fileFilter);

            List<File> arrayEntrys = Arrays.asList(filteredFiles);
            arrayFiles = new ArrayList<File>();
            for (File file : arrayEntrys) {
                if (file.canRead()) {
                    if (file.isFile()) {
                        arrayFiles.add(file);
                    } else if (file.isDirectory() && recursive) {
                        recursiveRetrieveFilesFromDir(arrayFiles, file, fileFilter);
                    }
                }
            }
            if (sorted) {
                Collections.sort(arrayEntrys);
            }
        }
        return arrayFiles;
    }

    public static List<File> retrieveFilesFromDir(File basedir, boolean recursive) {
        return retrieveFilesFromDir(basedir, new JFileChooser().getAcceptAllFileFilter(), recursive, false);
    }

    public static List<File> retrieveFilesFromDir(File basedir, FileFilter filter) {
        return retrieveFilesFromDir(basedir, filter, true, false);
    }

    public static List<File> retrieveFilesFromDir(File basedir) {
        return retrieveFilesFromDir(basedir, new JFileChooser().getAcceptAllFileFilter(), true, false);
    }

    private static void recursiveRetrieveFilesFromDir(List<File> files, File basedir, FilenameFilter filter) {
        File[] directoryEntries = basedir.listFiles(filter);
        for (int i = 0; i < directoryEntries.length; i++) {
            if (directoryEntries[i].canRead()) {
                if (directoryEntries[i].isFile()) {
                    files.add(directoryEntries[i]);
                } else if (directoryEntries[i].isDirectory()) {
                    recursiveRetrieveFilesFromDir(files, directoryEntries[i], filter);
                }
            }
        }
    }
}

Related

  1. promptSaveJFileChooser(String suggestedFileName, FileNameExtensionFilter... fileNameExtensionFilters)
  2. readAsString(File file)
  3. readAsString(File file)
  4. readFromFile()
  5. removeChoosableFileFilters(JFileChooser fc)
  6. run_file_chooser(String file_desc, String extension)
  7. save(JFileChooser fileChooser, Runnable action)
  8. saveFile(Component parent, File defaultFile)
  9. saveFile(String title, File currentDir, javax.swing.filechooser.FileFilter filter)