Java Resource Load getResourceStream(Class clazz, String resourceName)

Here you can find the source of getResourceStream(Class clazz, String resourceName)

Description

Returns a resource as a stream by checking: 1.

License

Open Source License

Parameter

Parameter Description
resourceName a parameter
path a parameter

Exception

Parameter Description
FileNotFoundException an exception

Declaration

public static InputStream getResourceStream(Class<?> clazz,
        String resourceName) throws FileNotFoundException 

Method Source Code

//package com.java2s;
/*//  ww  w  .j av  a 2  s  .  c om
 * Copyright (c) Mirth Corporation. All rights reserved.
 * 
 * http://www.mirthcorp.com
 * 
 * The software in this package is published under the terms of the MPL license a copy of which has
 * been included with this distribution in the LICENSE.txt file.
 */

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class Main {
    /**
     * Returns a resource as a stream by checking:
     * 
     * 1. The classpath for a resource with the specified name
     * 2. For a file with the specified path
     * 
     * @param resourceName
     * @param path
     * @return
     * @throws FileNotFoundException
     */
    public static InputStream getResourceStream(Class<?> clazz,
            String resourceName) throws FileNotFoundException {
        String cpResourceName = null;

        if (!resourceName.startsWith("/")) {
            cpResourceName = "/" + resourceName;
        } else {
            cpResourceName = resourceName;
        }

        InputStream is = clazz.getResourceAsStream(cpResourceName);

        if (is == null) {
            is = new FileInputStream(resourceName);
        }

        return is;
    }
}

Related

  1. getResourcesFromDirectory(final File directory, final Pattern pattern)
  2. getResourcesFromJarFile(File file, String resName)
  3. getResourcesFromJarFile(final File file, final Pattern pattern)
  4. getResourcesFromZip(final byte[] barContent)
  5. getResourceSize(ClassLoader classLoader, String path)
  6. getResourceStream(final String resource)
  7. getResourceStream(String className, ClassLoader classLoader)
  8. getResourceStream(String name)
  9. getResourceStream(String pathToResource)