Java Resource Get loadResource(final String name)

Here you can find the source of loadResource(final String name)

Description

Load given resource and return as an input stream.

License

BSD License

Parameter

Parameter Description
name - resource name

Exception

Parameter Description
IOException - Exception if we cannot read source

Return

InputStream

Declaration

public static InputStream loadResource(final String name) throws IOException 

Method Source Code


//package com.java2s;
/*// ww w . j a va2 s.  c o  m
 * $Id$
 * $URL$
 * 
 * =============================================================================
 * Ikasan Enterprise Integration Platform
 * 
 * Distributed under the Modified BSD License.
 * Copyright notice: The copyright for this software and a full listing 
 * of individual contributors are as shown in the packaged copyright.txt 
 * file. 
 * 
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 *
 *  - Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 *
 *  - Neither the name of the ORGANIZATION nor the names of its contributors may
 *    be used to endorse or promote products derived from this software without 
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * =============================================================================
 */

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class Main {
    /**
     * Load given resource and return as an input stream.
     * 
     * Firstly try loading from the classpath, if this fails try loading from the file system. If this fails throw an
     * IOException.
     * 
     * @param name - resource name
     * @return InputStream
     * @throws IOException - Exception if we cannot read source
     */
    public static InputStream loadResource(final String name) throws IOException {
        // Try loading via URL from the CLASSPATH
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        URL url = classLoader.getResource(name);
        if (url != null) {
            return url.openStream();
        }
        // Nothing on the CLASSPATH, lets try the file system
        return new FileInputStream(name);
    }
}

Related

  1. getResourcesFromDirectory(File resource, Pattern pattern)
  2. getResourceString(ResourceBundle rb, String key, Object param1)
  3. getResourceString(String key)
  4. getResourceString(String key, Object... args)
  5. loadResource(Class contextClass, String resourceName)
  6. loadResource(final String s)
  7. readResource(String resource, Class c)
  8. readToString(final Class nearClass, final String resource)
  9. readToString(final Class nearClass, final String resource, final String encoding)