集册 Java实例教程 从类路径加载资源,首先尝试线程上下文类加载器,然后尝试给定类的类加载器。

从类路径加载资源,首先尝试线程上下文类加载器,然后尝试给定类的类加载器。

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

488
从类路径加载资源,首先尝试线程上下文类加载器,然后尝试给定类的类加载器。


//package com.nowjava;

import java.io.InputStream;
//NowJava.com - 时代Java 提 供

public class Main {



    /**

     * Load a resource from the classpath, first trying the thread context

     * class loader, then the class loader of the given class.

     * @param clazz a class to try the class loader of

     * @param name the resource name

     * @return an input stream for reading the resource,

     * or null if not found

     */

    public static InputStream getResourceAsStream(Class clazz, String name) {

        InputStream in = Thread.currentThread().getContextClassLoader()

                .getResourceAsStream(name);

        
展开阅读全文