Java Resource Load getResource(Class c, String name)

Here you can find the source of getResource(Class c, String name)

Description

Similar to Class#getResourceAsStream(String) except looks up the parent hierarchy for the existence of the specified resource.

License

Apache License

Parameter

Parameter Description
c The class to return the resource on.
name The resource name.

Return

An input stream on the specified resource, or null if the resource could not be found.

Declaration

public static InputStream getResource(Class<?> c, String name) 

Method Source Code


//package com.java2s;
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *

import java.io.*;

public class Main {
    /**//from  w  ww. j a  v a 2  s.  c  o m
     * Similar to {@link Class#getResourceAsStream(String)} except looks up the
     * parent hierarchy for the existence of the specified resource.
     *
     * @param c The class to return the resource on.
     * @param name The resource name.
     * @return An input stream on the specified resource, or <jk>null</jk> if the resource could not be found.
     */
    public static InputStream getResource(Class<?> c, String name) {
        if (name == null)
            return null;
        while (c != null) {
            InputStream is = c.getResourceAsStream(name);
            if (is != null)
                return is;
            c = c.getSuperclass();
        }
        return null;
    }
}

Related

  1. getResource(Class c, String name)
  2. getResource(Class c, String name)
  3. getResource(Class c, String path)
  4. getResource(Class clazz, String fileName)
  5. getResource(Class clazz, String name, String charset)
  6. getResource(ClassLoader classLoader, final String path)
  7. getResource(final String aResName)
  8. getResource(final String resource)
  9. getResource(String fileName)