List of utility methods to do Resource Load
InputStream | getResourceAsInputStream(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; |
Properties | getResourceAsProperties(String resource) Resource as Properties return getResourceAsProperties(Thread.currentThread().getContextClassLoader(), resource);
|
Reader | getResourceAsReader(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); |
InputStreamReader | getResourceAsReader(String name) get Resource As Reader return new InputStreamReader(getResourceAsStream(name)); |
Reader | getResourceAsReader(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);
|
InputStream | getResourceAsStream(Class baseclass, String name) E.g. return baseclass.getResourceAsStream("resources/" + name + ".properties"); |
InputStream | getResourceAsStream(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); ... |
InputStream | getResourceAsStream(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; |
InputStream | getResourceAsStream(Class clazz, String resource) Gets a resource using the given classes classloader. return clazz.getClassLoader().getResourceAsStream(resource);
|
InputStream | getResourceAsStream(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; ... |