Java File List from Folder getFilesFromFolder(String folder, String mustHave, String doesntMustHave, boolean isPrevious)

Here you can find the source of getFilesFromFolder(String folder, String mustHave, String doesntMustHave, boolean isPrevious)

Description

Gets the files from folder.

License

Mozilla Public License

Parameter

Parameter Description
folder the folder
mustHave the must have
doesntMustHave the doesnt must have
isPrevious the is previous

Exception

Parameter Description
IOException Signals that an I/O exception has occurred.
Exception the exception

Return

the files from folder

Declaration

private static HashSet<String> getFilesFromFolder(String folder, String mustHave, String doesntMustHave,
        boolean isPrevious) throws IOException, Exception 

Method Source Code

//package com.java2s;
/**//from   w  w w.j ava2 s.  c o  m
 * Copyright (c) 2016 TermMed SA
 * Organization
 *
 * 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.io.IOException;

import java.util.HashSet;

public class Main {
    /**
     * Gets the files from folder.
     *
     * @param folder the folder
     * @param mustHave the must have
     * @param doesntMustHave the doesnt must have
     * @param isPrevious the is previous
     * @return the files from folder
     * @throws IOException Signals that an I/O exception has occurred.
     * @throws Exception the exception
     */
    private static HashSet<String> getFilesFromFolder(String folder, String mustHave, String doesntMustHave,
            boolean isPrevious) throws IOException, Exception {
        HashSet<String> result = new HashSet<String>();
        File dir = new File(folder);
        HashSet<String> files = new HashSet<String>();
        findAllFiles(dir, files, mustHave, doesntMustHave, isPrevious);
        result.addAll(files);
        return result;

    }

    /**
     * Find all files.
     *
     * @param releaseFolder the release folder
     * @param hashSimpleRefsetList the hash simple refset list
     * @param mustHave the must have
     * @param doesntMustHave the doesnt must have
     * @param isPrevious the is previous
     */
    public static void findAllFiles(File releaseFolder, HashSet<String> hashSimpleRefsetList, String mustHave,
            String doesntMustHave, boolean isPrevious) {
        String name = "";
        if (hashSimpleRefsetList == null) {
            hashSimpleRefsetList = new HashSet<String>();

        }
        for (File file : releaseFolder.listFiles()) {
            if (file.isDirectory()) {
                findAllFiles(file, hashSimpleRefsetList, mustHave, doesntMustHave, isPrevious);
            } else {
                name = file.getName().toLowerCase();
                if (mustHave != null && !name.contains(mustHave.toLowerCase())) {
                    continue;
                }
                if (doesntMustHave != null && name.contains(doesntMustHave.toLowerCase())) {
                    continue;
                }
                if (isPrevious && !name.contains("_pre")) {
                    continue;
                }
                if (!isPrevious && name.contains("_pre")) {
                    continue;
                }
                if (name.endsWith(".txt")) {
                    hashSimpleRefsetList.add(file.getAbsolutePath());
                }
            }
        }

    }
}

Related

  1. getFilesFromFolder(String folderPath, ArrayList files)
  2. getFilesFromFolder(String folderPath, boolean withAbsolPath)