Java Utililty Methods Resource Load

List of utility methods to do Resource Load

Description

The list of methods to do Resource Load are organized into topic(s).

Method

InputStreamgetResourceAsInputStream(Class resourceClass, String resourceName)
get Resource As Input Stream
InputStream inputStream = resourceClass.getResourceAsStream(resourceName);
if (inputStream == null) {
    throw new IllegalStateException("Unable to find resource [" + resourceName + "] relative to class ["
            + resourceClass.getName() + "].");
return inputStream;
PropertiesgetResourceAsProperties(String resource)
Resource as Properties
return getResourceAsProperties(Thread.currentThread().getContextClassLoader(), resource);
ReadergetResourceAsReader(final Class classLocation, final String resourceName)
Get a ressource in classpath and convert it as Reader.
InputStream in = classLocation.getResourceAsStream(resourceName);
if (null == in) {
    throw new IOException("Resource not found : " + resourceName);
return new InputStreamReader(in);
InputStreamReadergetResourceAsReader(String name)
get Resource As Reader
return new InputStreamReader(getResourceAsStream(name));
ReadergetResourceAsReader(String relativeName, Class clazz, String enc)
Returns the reader for the resource with the specified relative name.
String newName = getAbsoluteResourceName(relativeName, clazz);
return getResourceAsReader(newName, enc);
InputStreamgetResourceAsStream(Class baseclass, String name)
E.g.
return baseclass.getResourceAsStream("resources/" + name + ".properties");
InputStreamgetResourceAsStream(Class claz, String name)
Finds a resource with the given name.
InputStream result = null;
while (name.startsWith("/")) {
    name = name.substring(1);
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
    classLoader = claz.getClassLoader();
    result = classLoader.getResourceAsStream(name);
...
InputStreamgetResourceAsStream(Class clazz, String name)
Load a resource from the classpath, first trying the thread context class loader, then the class loader of the given class.
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
if (in == null) {
    in = clazz.getResourceAsStream(name);
return in;
InputStreamgetResourceAsStream(Class clazz, String resource)
Gets a resource using the given classes classloader.
return clazz.getClassLoader().getResourceAsStream(resource);
InputStreamgetResourceAsStream(Class clazz, String resource, boolean checkThreadContextFirst)
Get an input stream from a named resource.
InputStream myInputStream = null;
if (checkThreadContextFirst && Thread.currentThread().getContextClassLoader() != null) {
    myInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
if (myInputStream == null) {
    myInputStream = getResourceAsStream(clazz, resource);
return myInputStream;
...