Java Resource Load getResourceAsStream(Class clazz, String name)

Here you can find the source of getResourceAsStream(Class clazz, String name)

Description

Load a resource from the classpath, first trying the thread context class loader, then the class loader of the given class.

License

Apache License

Parameter

Parameter Description
clazz a class to try the class loader of
name the resource name

Return

an input stream for reading the resource, or null if not found

Declaration

public static InputStream getResourceAsStream(Class clazz, String name) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.InputStream;

public class Main {
    /**//from w ww .j  ava2s. c om
     * Load a resource from the classpath, first trying the thread context
     * class loader, then the class loader of the given class.
     * @param clazz a class to try the class loader of
     * @param name the resource name
     * @return an input stream for reading the resource,
     * or null if not found
     */
    public static InputStream getResourceAsStream(Class clazz, String name) {
        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
        if (in == null) {
            in = clazz.getResourceAsStream(name);
        }
        return in;
    }
}

Related

  1. getResourceAsReader(final Class classLocation, final String resourceName)
  2. getResourceAsReader(String name)
  3. getResourceAsReader(String relativeName, Class clazz, String enc)
  4. getResourceAsStream(Class baseclass, String name)
  5. getResourceAsStream(Class claz, String name)
  6. getResourceAsStream(Class clazz, String resource)
  7. getResourceAsStream(Class clazz, String resource, boolean checkThreadContextFirst)
  8. getResourceAsStream(Class clazz, String fileName)
  9. getResourceAsStream(Class clazz, String resource)