Java Resource Load getResourceAsStream(String path, Class caller)

Here you can find the source of getResourceAsStream(String path, Class caller)

Description

get Resource As Stream

License

Apache License

Declaration

public static InputStream getResourceAsStream(String path, Class<?> caller) 

Method Source Code


//package com.java2s;
/*/*from  ww  w .j  a  va2s  . c o m*/
 * Copyright 2005-2010 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.InputStream;

public class Main {
    private static final String SLASH = "/";

    public static InputStream getResourceAsStream(String path, Class<?> caller) {

        String resource = path;
        if (resource.startsWith(SLASH)) {
            resource = resource.substring(1);
        }

        InputStream is = null;
        File file = new File(path);
        if (file.exists() && file.isFile()) {
            try {
                is = new FileInputStream(file);
            } catch (FileNotFoundException e) {
            }
        }
        if (is == null) {
            ClassLoader tcl = Thread.currentThread().getContextClassLoader();
            if (tcl != null) {
                is = tcl.getResourceAsStream(resource);
            }
        }
        if (is == null) {
            is = caller.getResourceAsStream(path);
        }
        if (is == null) {
            is = ClassLoader.class.getResourceAsStream(path);
        }
        if (is == null) {
            is = ClassLoader.getSystemResourceAsStream(resource);
        }

        return is;
    }
}

Related

  1. getResourceAsStream(String name, Class aClass)
  2. getResourceAsStream(String name, Class clazz)
  3. getResourceAsStream(String name, Class clazz)
  4. getResourceAsStream(String name, Class clzz)
  5. getResourceAsStream(String path)
  6. getResourceAsStream(String path, ClassLoader cl)
  7. getResourceAsStream(String path, ClassLoader loader)
  8. getResourceAsStream(String relativeName, Class clazz)
  9. getResourceAsStream(String res)