Java Folder Read getFilesFromDiskWorker(File folderToLoad, final String searchPattern, final boolean justFolders)

Here you can find the source of getFilesFromDiskWorker(File folderToLoad, final String searchPattern, final boolean justFolders)

Description

Get the list of sorted files in folder or null if parent folder doesn't exist or pattern is not valid.

License

Open Source License

Parameter

Parameter Description
folderToLoad a parameter
searchPattern a parameter
justFolders a parameter

Declaration

private static File[] getFilesFromDiskWorker(File folderToLoad, final String searchPattern,
        final boolean justFolders) 

Method Source Code

//package com.java2s;
/*/* w w w. j  a v  a  2  s  .c o  m*/
 * Copyright (c) 2004-2016 Universidade do Porto - Faculdade de Engenharia
 * Laborat?rio de Sistemas e Tecnologia Subaqu?tica (LSTS)
 * All rights reserved.
 * Rua Dr. Roberto Frias s/n, sala I203, 4200-465 Porto, Portugal
 *
 * This file is part of Neptus, Command and Control Framework.
 *
 * Commercial Licence Usage
 * Licencees holding valid commercial Neptus licences may use this file
 * in accordance with the commercial licence agreement provided with the
 * Software or, alternatively, in accordance with the terms contained in a
 * written agreement between you and Universidade do Porto. For licensing
 * terms, conditions, and further information contact lsts@fe.up.pt.
 *
 * European Union Public Licence - EUPL v.1.1 Usage
 * Alternatively, this file may be used under the terms of the EUPL,
 * Version 1.1 only (the "Licence"), appearing in the file LICENCE.md
 * included in the packaging of this file. You may not use this work
 * except in compliance with the Licence. Unless required by applicable
 * law or agreed to in writing, software distributed under the Licence is
 * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF
 * ANY KIND, either express or implied. See the Licence for the specific
 * language governing permissions and limitations at
 * http://ec.europa.eu/idabc/eupl.html.
 *
 * For more information please see <http://lsts.fe.up.pt/neptus>.
 *
 * Author: Paulo Dias
 * 2005/01/14
 */

import java.io.File;

import java.io.FilenameFilter;

import java.util.Arrays;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**
     * Get the list of sorted files in folder or null if parent folder doesn't exist or pattern is not valid.
     * This is the worker used by {@link #getFilesFromDisk(File, String)} and {@link #getFoldersFromDisk(File, String)}.
     * 
     * @param folderToLoad
     * @param searchPattern
     * @param justFolders
     * @return
     */
    private static File[] getFilesFromDiskWorker(File folderToLoad, final String searchPattern,
            final boolean justFolders) {
        try {
            if (folderToLoad != null && folderToLoad.exists()) {
                File folder = folderToLoad.isDirectory() ? folderToLoad : folderToLoad.getParentFile();

                FilenameFilter fileFilter = new FilenameFilter() {
                    Pattern pat = searchPattern == null || searchPattern.isEmpty() ? null
                            : Pattern.compile(searchPattern);

                    @Override
                    public boolean accept(File file, String name) {
                        if (pat == null) {
                            File fx = new File(file, name);
                            return justFolders ? fx.isDirectory() : fx.isFile();
                        } else {
                            Matcher m = pat.matcher(name);
                            boolean ret = m.find();
                            File fx = new File(file, name);
                            return ret ? (justFolders ? fx.isDirectory() : fx.isFile()) : false;
                        }
                    }
                };

                File[] lst = folder.listFiles(fileFilter);
                Arrays.sort(lst);
                return lst;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. getFilesFor(List patientSets, File inputDirectory)
  2. getFilesForDirectory(File dir)
  3. getFilesForFolder(File folder)
  4. getFilesForPattern(String pathToScan, String startWith, String endsWith)
  5. getFilesFromClassPath(String classpath)
  6. getFileSimpleName(final File f)
  7. getFilesInClassPath(String path, String endsWith)
  8. getFilesIncludeSubdirectories(final File directory)
  9. getFilesInFolder(File dir)