Java ClassPath Get getClassPathResourcesAsStreams(String relativeResourceName)

Here you can find the source of getClassPathResourcesAsStreams(String relativeResourceName)

Description

Search resources within the classpath that match the given name, create input streams from the found resources and return them as a list.

License

Open Source License

Parameter

Parameter Description
relativeResourceName resource name relative to the root elements of the classpath (usually the jar-files root element, or the folder specified in the classpath)

Return

a List of input streams matching the resource name

Declaration

public static List<InputStream> getClassPathResourcesAsStreams(String relativeResourceName) 

Method Source Code

//package com.java2s;
/*//from w w w  .ja v  a2s .co  m
 * JBoss, Home of Professional Open Source.
 * Copyright 2007, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This 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 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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 this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import java.io.IOException;
import java.io.InputStream;

import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class Main {
    /**
     * Search resources within the classpath that match the given name, create input streams
     * from the found resources and return them as a list.
     * 
     * @param relativeResourceName resource name relative to the root elements of the classpath 
     *                 (usually the jar-files root element, or the folder specified in the classpath)
     * @return a List<InputStream> of input streams matching the resource name
     */
    public static List<InputStream> getClassPathResourcesAsStreams(String relativeResourceName) {
        List<InputStream> result = new ArrayList<InputStream>();
        try {
            Enumeration<URL> resourceUrls = Thread.currentThread().getContextClassLoader()
                    .getResources(relativeResourceName);
            while (resourceUrls.hasMoreElements()) {
                result.add(((InputStream) (resourceUrls.nextElement().getContent())));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException npe) {
            npe.printStackTrace();
        }
        return result;
    }
}

Related

  1. getClassPathPath()
  2. getClasspathResource(final String resource)
  3. getClasspathResource(String name)
  4. getClasspathResource(String path)
  5. getClasspathResourceAsFile(String resourceName)
  6. getClassPathRoot(Class cl)
  7. getClasspathString()