Java Utililty Methods Resource Get

List of utility methods to do Resource Get

Description

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

Method

FilegetResource(String name)
get Resource
URI uri = locate(name);
if (uri == null) {
    return null;
return new File(uri);
URLgetResource(String name)
Determines the url of the indicated resource using the currently set class loader.
return getClassLoader().getResource(name);
URLgetResource(String name)
Get URL name of resource and check if it exists somewhere in the classpath.
URL url = Thread.currentThread().getContextClassLoader().getResource(name);
if (url == null) {
    throw new IOException("could not find " + name + " in classpath");
return url;
URLgetResource(String name)
get Resource
ClassLoader cl = getClassLoader();
if (cl == null) {
    return null;
return cl.getResource(name);
URLgetResource(String name)
Resources are those stored in the class path (e.g., src/main/resources).
ClassLoader loader = Thread.currentThread().getContextClassLoader();
return loader.getResource(name);
URLgetResource(String name, Class neighbor)
get Resource
if (neighbor == null) {
    return getResource(name);
return getResource(resolveName(neighbor, name), neighbor.getClassLoader());
URLgetResource(String name, Class caller)
get Resource
ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
if (threadClassLoader != null) {
    URL url = threadClassLoader.getResource(name);
    if (url != null)
        return url;
ClassLoader callerClassLoader = caller.getClassLoader();
return callerClassLoader.getResource(name);
...
InputStreamgetResource(String res)
get Resource
URL tokensResource = ClassLoader.getSystemClassLoader().getResource(res);
FileInputStream is;
try {
    is = new FileInputStream(new File(tokensResource.toURI()));
    return is;
} catch (URISyntaxException e) {
    throw new RuntimeException(e);
URLgetResource(String resource, Class context)
Loads a URL resource from the classloader; If not found, the classloader of the context class specified will be used.
return getResource(resource, context, false);
byte[]getResourceAsBytes(String resource)
get Resource As Bytes
ClassLoader cl = Thread.currentThread().getContextClassLoader();
URL url = cl.getResource(resource);
InputStream is = url.openStream();
byte[] data = getStreamContentAsBytes(is);
is.close();
return data;