Java Resource Load getResourceAsStream(String resource, ClassLoader classLoader)

Here you can find the source of getResourceAsStream(String resource, ClassLoader classLoader)

Description

Returns a resource on the classpath as a Stream object.

License

Apache License

Parameter

Parameter Description
resource The resource to find
classLoader the class loader

Exception

Parameter Description
IOException If the resource cannot be found or read

Return

The resource

Declaration

public static InputStream getResourceAsStream(String resource, ClassLoader classLoader) throws IOException 

Method Source Code

//package com.java2s;
/**//from   w ww .  j a  va  2  s.com
 * Copyright 2008-2016 Juho Jeong
 *
 * 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.
 */

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

public class Main {
    /**
     * Returns a resource on the classpath as a Stream object.
     *
     * @param resource The resource to find
     * @param classLoader the class loader
     * @return The resource
     * @throws IOException If the resource cannot be found or read
     */
    public static InputStream getResourceAsStream(String resource, ClassLoader classLoader) throws IOException {
        InputStream in = null;
        if (classLoader != null)
            in = classLoader.getResourceAsStream(resource);
        if (in == null)
            in = ClassLoader.getSystemResourceAsStream(resource);
        if (in == null)
            throw new IOException("Could not find resource " + resource);
        return in;
    }
}

Related

  1. getResourceAsStream(String path, ClassLoader loader)
  2. getResourceAsStream(String relativeName, Class clazz)
  3. getResourceAsStream(String res)
  4. getResourceAsStream(String resName, Object obj)
  5. getResourceAsStream(String resource, ClassLoader classLoader)
  6. getResourceAsStream(String resourcePath)
  7. getResourceAsStreamInClassPath(@SuppressWarnings("rawtypes") Class clazz, String filepath)
  8. getResourceAsStrem(String name)
  9. getResourceAsString(Class clazz, String name)