Java Folder Read getFilesRegex(final File root, final String[] regex)

Here you can find the source of getFilesRegex(final File root, final String[] regex)

Description

Returns recursively all files in a directory that have a path whose suffix beyond root is matched by regex.

License

Open Source License

Parameter

Parameter Description
root a parameter
regex a parameter

Declaration

public static Collection<File> getFilesRegex(final File root, final String[] regex) 

Method Source Code


//package com.java2s;
/*//from   w w w  .  jav  a 2 s . c  o  m
 * Copyright (C) 2015 Daniel Dietsch (dietsch@informatik.uni-freiburg.de)
 * Copyright (C) 2015 University of Freiburg
 * 
 * This file is part of the ULTIMATE licence-manager.
 * 
 * The ULTIMATE licence-manager is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * The ULTIMATE licence-manager 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 Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the ULTIMATE licence-manager. If not, see <http://www.gnu.org/licenses/>.
 * 
 * Additional permission under GNU GPL version 3 section 7:
 * If you modify the ULTIMATE licence-manager, or any covered work, by linking
 * or combining it with Eclipse RCP (or a modified version of Eclipse RCP), 
 * containing parts covered by the terms of the Eclipse Public License, the 
 * licensors of the ULTIMATE licence-manager grant you additional permission 
 * to convey the resulting work.
 */

import java.io.File;

import java.util.ArrayList;
import java.util.Collection;

public class Main {
    /**
     * Returns recursively all files in a directory that have a path whose
     * suffix beyond root is matched by regex. If root is a file, a collection
     * containing root is returned (ignoring the regex) E.g., your file root has
     * the absolute path /home/horst/ultimate/ and your regex is *horst* you
     * obtain the files that contain the String "horst" if the prefix
     * "/home/horst/ultimate/" was removed.
     * 
     * @param root
     * @param regex
     * @return
     */
    public static Collection<File> getFilesRegex(final File root, final String[] regex) {
        return getFilesRegex(root.getAbsolutePath(), root, regex);
    }

    /**
     * Returns recursively all files in a directory that have a path whose
     * suffix beyond the String prefix is matched by regex. If root is a file, a
     * collection containing root is returned (ignoring the regex).
     * 
     * @param root
     * @param regex
     * @return
     */
    private static Collection<File> getFilesRegex(final String prefix, final File root, final String[] regex) {
        if (!root.getAbsolutePath().startsWith(prefix)) {
            throw new IllegalArgumentException("prefix is no prefix of root.getAbsolutePath()");
        }
        final ArrayList<File> rtr = new ArrayList<File>();

        if (root.isFile()) {
            rtr.add(root);
            return rtr;
        }

        final File[] list = root.listFiles();

        if (list == null) {
            return rtr;
        }

        for (final File f : list) {
            if (f.isDirectory()) {
                rtr.addAll(getFilesRegex(prefix, f, regex));
            } else {
                if (regex == null || regex.length == 0) {
                    rtr.add(f);
                } else {
                    for (final String s : regex) {
                        final String suffix = f.getAbsolutePath().substring(prefix.length());
                        if (suffix.matches(s)) {
                            rtr.add(f);
                            break;
                        }
                    }
                }
            }
        }
        return rtr;
    }
}

Related

  1. getFilesMatchingRegexp(final File directory, final String regexp)
  2. getFilesModDate(String path)
  3. getFilesOf(File dir)
  4. getFilesOfDirectory(File directory)
  5. getFilesOfTypeInDirectory(File directory, String filetype)
  6. getFilesStartingWith(File parentDir, String prefix)
  7. getFilesStartingWith(String dirName, String startsWith)
  8. getFileStatus(File file)
  9. getFileString(File file)