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

InputStreamgetResourceStreamFromClasspath(Class clazz, String resourceName)
Helper method for grabbing a file from the classpath and returning an InputStream
InputStream is = clazz.getResourceAsStream(resourceName);
if (is == null) {
    is = clazz.getClassLoader().getResourceAsStream(resourceName);
    if (is == null)
        throw new RuntimeException("resource not found in classpath: " + resourceName + " class is: "
                + clazz.getCanonicalName());
return is;
...
InputStreamgetResourceStreamInPackage(Class clazz, String name)
get Resource Stream In Package
return clazz.getResourceAsStream("/" + clazz.getPackage().getName().replace(".", "/") + "/" + name);
StringgetResourceString(String filename)
get Resource String
String rootPath = "./Oracle/src/test/resources/co/digitaloracle/";
String string = new Scanner(new File(rootPath + filename)).useDelimiter("\\Z").next();
return string;
URIgetResourceUri(final String packageToScan, final ClassLoader classLoader)
get Resource Uri
String folderToScan = packageToScan.replace(PACKAGE_SEPARATOR, RESOURCE_SEPARATOR);
URL url = classLoader.getResource(folderToScan);
if (url == null) {
    throw new IllegalArgumentException("No folder to scan found for package '" + packageToScan + "'.");
try {
    if (url.getPath().contains(" ")) {
        url = new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getPath().replace(" ", "%20"));
...
URLgetResourceUrl(final Class aClass, final String aPath)
get Resource Url
final URL url = aClass.getResource(aPath);
if (url == null) {
    throw new FileNotFoundException("Resource not found for " + aClass.toString() + ": " + aPath);
} else {
    return url;
URLgetResourceUrl(final String resourcePath)
Gets the URL of a local resource.
return ClassLoader.class.getResource(resourcePath);
URLgetResourceUrl(String path, String extension, ClassLoader loader)
get Resource Url
if (path == null || loader == null) {
    return null;
path = getResourcePath(path, extension);
return loader.getResource(path);
URLgetResourceURL(String resourcePath)
Get an URL of a classpath-relative resource.
final ClassLoader cl = Thread.currentThread().getContextClassLoader();
final URL url = cl.getResource(resourcePath);
if (url == null)
    throw new IOException("Resource not found: " + resourcePath);
return url;
URLgetResourceUrl(String resourcePath)
Returns the specified resource URL or null if not found.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
return classLoader.getResource(resourcePath);
byte[]getResourceUsingFileStreams(InputStream source)
get Resource Using File Streams
ByteArrayOutputStream output = null;
try {
    output = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int bytesRead;
    while ((bytesRead = source.read(buf)) > 0) {
        output.write(buf, 0, bytesRead);
    output.flush();
    return output.toByteArray();
} catch (Exception ex) {
    return null;
} finally {
    try {
        source.close();
        output.close();
    } catch (Exception ex) {