集册 Java实例教程 这是一种将资源加载为流的便捷方法。

这是一种将资源加载为流的便捷方法。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

509
这是一种将资源加载为流的便捷方法。
/**来自 时代Java公众号 - N o w J a  v a . c o m**/


import java.io.*;

import java.net.URISyntaxException;

import java.net.URL;

import java.net.URLDecoder;

import java.util.*;

import java.util.jar.JarEntry;

import java.util.jar.JarFile;


public class Main{

    public static void main(String[] argv) throws Exception{

        String name = "nowjava.com";

        System.out.println(getResourceAsStream(name));

    }

    /**

     * This is a convenience method to load a resource as a stream.

     *

     * The algorithm used to find the resource is given in getResource()

     *

     * @param resourceName The name of the resource to load

     * @param callingClass The Class object of the calling object

     * @return  the InputStream of the specific resource on the 'resources' folder.

     */

    public static InputStream getResourceAsStream(String resourceName,

            Class<?> callingClass) {

        URL url = getResourceAsURL(resourceName, callingClass);

        try {/** 来 自 n o w j a v a . c o m - 时代Java**/

            return (url != null) ? url.openStream() : null;

        } catch (IOException e) {

            return null;

        }

    }

    public static InputStream getResourceAsStream(String name) {

        name = resolveName(name);

        ClassLoader cl = ClassLoader.getSystemClassLoader();

        if (cl == null) {

            // A system class.

            return ClassLoader.getSystemResourceAsStream(name);

        }

        return cl.getResourceAsStream(name);

    }

    /**

     * Load a given resource.

     *

     * This method will try to load the resource using the following methods (in order):

     * <ul>

     *  <li>From Thread.currentThread().getContextClassLoader()

     *  <li>From ClassLoaderUtil.class.getClassLoader()

     *  <li>callingClass.getClassLoader()

     * </ul>

     *

     * @param resourceName The name IllegalStateException("Unable to call ")of the resource to load

     * @param callingClass The Class object of the calling object

     * @return the URL of the specific resource on the 'resources' folder.

     */

    public static URL getResourceAsURL(String resourceName,

            Class<?> callingClass) {

        URL url = Thread.currentThread().getContextClassLoader()

                .getResource(resourceName);


        if (url == null) {

            url = ClassLoaderUtil.class.getClassLoader().getResource(

                    resourceName);

        }

        if (url == null) {

            ClassLoader cl = callingClass.getClassLoader();


            if (cl != null) {

                url = cl.getResource(resourceName);

            }

        }


        if ((url == null)

                && (resourceName != null)

                && ((resourceName.length() == 0) || (resourceName.charAt(0) != '/'))) {

            return getResourceAsURL('/' + resourceName, callingClass);

        }


        return url;

    }

    /**

     * Add a package name prefix if the name is not absolute Remove leading "/"

     * if name is absolute

     * @param name the {@link String} of the resources.

     * @return the {@link String} new name.

     */

    private static String resolveName(String name) {

        if (name == null)

            return null;

        
展开阅读全文