Java Resource Load getResourceNamesFromDir(File dir, String extension)

Here you can find the source of getResourceNamesFromDir(File dir, String extension)

Description

get Resource Names From Dir

License

Apache License

Parameter

Parameter Description
dir a parameter
extension a parameter

Return

A list of paths relative to dir of the files from dir with the provided filename extension. The directory is traversed recursively.

Declaration

public static List<String> getResourceNamesFromDir(File dir, String extension) 

Method Source Code

//package com.java2s;
/*/* w  w w  .j  av  a2  s  .com*/
 * #%L
 * ELK Utilities for Testing
 * 
 * $Id$
 * $HeadURL$
 * %%
 * Copyright (C) 2011 - 2012 Department of Computer Science, University of Oxford
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.io.File;

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

import java.util.List;

public class Main {
    /**
     * @param dir
     * @param extension
     * @return A list of paths relative to {@code dir} of the files from
     *         {@code dir} with the provided filename extension. The directory
     *         is traversed recursively.
     */
    public static List<String> getResourceNamesFromDir(File dir, String extension) {
        List<String> testResources = new ArrayList<String>();
        collectResourceNamesFromDir(dir, "", extension, testResources);
        return testResources;
    }

    private static void collectResourceNamesFromDir(final File file, final String prefix, final String extension,
            final Collection<String> result) {

        final String fileName = file.getName();

        if (file.isDirectory()) {
            final File[] innerFiles = file.listFiles();
            if (innerFiles == null) {
                throw new RuntimeException("Error listing directory " + file);
            }
            // else
            for (final File innerFile : innerFiles) {
                collectResourceNamesFromDir(innerFile, prefix + fileName + File.separator, extension, result);
            }
            return;
        }
        // else file is not a directory

        if (fileName.endsWith("." + extension)) {
            result.add(prefix + fileName);
        }

    }
}

Related

  1. getResourceList(File fdir, String extn)
  2. getResourceListing(URL pathUrl, String prefix, String suffix)
  3. getResourceNameFromFileName(final String pFileName)
  4. getResourceNames(File dir)
  5. getResourceNames(String resName)
  6. getResourcePath()
  7. getResourcePath(Class c, String name)
  8. getResourceReader(final String aResName)
  9. getResources(File root, File path, Vector result)