我的Java开发学习之旅------>JAVA 笔记ClassLoader.getResourceAsStream() 与 Class.getResourceAsStream()的区别

今天,一读者在我的 Android通过调用Webservice实现手机号码归属地查询 文章中给我回复了一个问题,由于没有具体说明我的sop12.xml文件是放在src目录下,不是和具体操作类AddressService.java放在同一目录下,引起了误会。


  1. InputStream inputStream = AddressService.class.getClassLoader()  
  2.                 .getResourceAsStream("sop12.xml");  




现在在这里总结一下Java中getResourceAsStream的用法。

首先,Java中的getResourceAsStream有以下几种:
1. Class.getResourceAsStream(String path) : path 不以’/'开头时默认是从此类所在的包下取资源,以’/'开头则是从

ClassPath根下获取。其只是通过path构造一个绝对路径,最终还是由ClassLoader获取资源。

2. Class.getClassLoader.getResourceAsStream(String path) :默认则是从ClassPath根下获取,path不能以’/'开头,最终是由

ClassLoader获取资源。

3. ServletContext. getResourceAsStream(String path):默认从WebAPP根目录下取资源,Tomcat下path是否以’/'开头无所谓,

当然这和具体的容器实现有关。

4. Jsp下的application内置对象就是上面的ServletContext的一种实现。

其次,getResourceAsStream 用法大致有以下几种:

第一: 要加载的文件和.class文件在同一目录下,例如:com.x.y 下有类me.class ,同时有资源文件myfile.xml

那么,应该有如下代码:

me.class.getResourceAsStream("myfile.xml");

第二:在me.class目录的子目录下,例如:com.x.y 下有类me.class ,同时在 com.x.y.file 目录下有资源文件myfile.xml

那么,应该有如下代码:

me.class.getResourceAsStream("file/myfile.xml");

第三:不在me.class目录下,也不在子目录下,例如:com.x.y 下有类me.class ,同时在 com.x.file 目录下有资源文件myfile.xml

那么,应该有如下代码:

me.class.getResourceAsStream("/com/x/file/myfile.xml");

总结一下,可能只是两种写法

第一:前面有 “ / ”

“ / ”代表了工程的根目录,例如工程名叫做myproject,“ / ”代表了myproject

me.class.getResourceAsStream("/com/x/file/myfile.xml");

第二:前面没有 “ / ”

代表当前类的目录

me.class.getResourceAsStream("myfile.xml");

me.class.getResourceAsStream("file/myfile.xml"); 

最后,自己的理解: 
getResourceAsStream读取的文件路径只局限与工程的源文件夹中,包括在工程src根目录下,以及类包里面任何位置,但是如果配置文件路径是在除了源文件夹之外的其他文件夹中时,该方法是用不了的。 




附注:java.lang.ClassLoader.gClassLoader()源码

 /**
     * Returns an input stream for reading the specified resource.
     *
     * <p> The search order is described in the documentation for {@link
     * #getResource(String)}.  </p>
     *
     * @param  name
     *         The resource name
     *
     * @return  An input stream for reading the resource, or <tt>null</tt>
     *          if the resource could not be found
     *
     * @since  1.1
     */
    public InputStream getResourceAsStream(String name) {
	URL url = getResource(name);
	try {
	    return url != null ? url.openStream() : null;
	} catch (IOException e) {
	    return null;
	}
    }
java.lang. ClassLoader.getResource()源码

/**
     * Finds the resource with the given name.  A resource is some data
     * (images, audio, text, etc) that can be accessed by class code in a way
     * that is independent of the location of the code.
     *
     * <p> The name of a resource is a '<tt>/</tt>'-separated path name that
     * identifies the resource.
     *
     * <p> This method will first search the parent class loader for the
     * resource; if the parent is <tt>null</tt> the path of the class loader
     * built-in to the virtual machine is searched.  That failing, this method
     * will invoke {@link #findResource(String)} to find the resource.  </p>
     *
     * @param  name
     *         The resource name
     *
     * @return  A <tt>URL</tt> object for reading the resource, or
     *          <tt>null</tt> if the resource could not be found or the invoker
     *          doesn't have adequate  privileges to get the resource.
     *
     * @since  1.1
     */
    public URL getResource(String name) {
	URL url;
	if (parent != null) {
	    url = parent.getResource(name);
	} else {
	    url = getBootstrapResource(name);
	}
	if (url == null) {
	    url = findResource(name);
	}
	return url;
    }

java.lang.Class.getResourceAsStream(String name)源码

/**
     * Finds a resource with a given name.  The rules for searching resources
     * associated with a given class are implemented by the defining
     * {@linkplain ClassLoader class loader} of the class.  This method
     * delegates to this object's class loader.  If this object was loaded by
     * the bootstrap class loader, the method delegates to {@link
     * ClassLoader#getSystemResourceAsStream}.
     *
     * <p> Before delegation, an absolute resource name is constructed from the
     * given resource name using this algorithm:
     *
     * <ul>
     *
     * <li> If the <tt>name</tt> begins with a <tt>'/'</tt>
     * (<tt>'\u002f'</tt>), then the absolute name of the resource is the
     * portion of the <tt>name</tt> following the <tt>'/'</tt>. 
     *
     * <li> Otherwise, the absolute name is of the following form:
     *
     * <blockquote><pre>
     *   <tt>modified_package_name</tt>/<tt>name</tt>
     * </pre></blockquote>
     *
     * <p> Where the <tt>modified_package_name</tt> is the package name of this
     * object with <tt>'/'</tt> substituted for <tt>'.'</tt>
     * (<tt>'\u002e'</tt>).
     *
     * </ul>
     *
     * @param  name name of the desired resource
     * @return      A {@link java.io.InputStream} object or <tt>null</tt> if
     *              no resource with this name is found
     * @throws  NullPointerException If <tt>name</tt> is <tt>null</tt>
     * @since  JDK1.1
     */
     public InputStream getResourceAsStream(String name) {
        name = resolveName(name);
        ClassLoader cl = getClassLoader0();
        if (cl==null) {
            // A system class.
            return ClassLoader.getSystemResourceAsStream(name);
        }
        return cl.getResourceAsStream(name);
    }


java.lang.Class.getResource(String name)源码

/**
     * Finds a resource with a given name.  The rules for searching resources
     * associated with a given class are implemented by the defining
     * {@linkplain ClassLoader class loader} of the class.  This method
     * delegates to this object's class loader.  If this object was loaded by
     * the bootstrap class loader, the method delegates to {@link
     * ClassLoader#getSystemResource}.
     *
     * <p> Before delegation, an absolute resource name is constructed from the
     * given resource name using this algorithm:
     *
     * <ul>
     *
     * <li> If the <tt>name</tt> begins with a <tt>'/'</tt>
     * (<tt>'\u002f'</tt>), then the absolute name of the resource is the
     * portion of the <tt>name</tt> following the <tt>'/'</tt>. 
     *
     * <li> Otherwise, the absolute name is of the following form:
     *
     * <blockquote><pre>
     *   <tt>modified_package_name</tt>/<tt>name</tt>
     * </pre></blockquote>
     *
     * <p> Where the <tt>modified_package_name</tt> is the package name of this
     * object with <tt>'/'</tt> substituted for <tt>'.'</tt>
     * (<tt>'\u002e'</tt>).
     *
     * </ul>
     *
     * @param  name name of the desired resource
     * @return      A  {@link java.net.URL} object or <tt>null</tt> if no
     *              resource with this name is found
     * @since  JDK1.1
     */
    public java.net.URL getResource(String name) {
        name = resolveName(name);
        ClassLoader cl = getClassLoader0();
        if (cl==null) {
            // A system class.
            return ClassLoader.getSystemResource(name);
        }
        return cl.getResource(name);
    }


从中可以看得出来:Class.getResource()和Class.getResourceAsStream()方法
查找与给定类相关的资源的规则是通过定义类的 class loader 实现的。此方法委托此对象的类加载器。如果此对象通过引导类加载器加载,则此方法将委托给 ClassLoader.getSystemResourceAsStream(java.lang.String)。和 ClassLoader.getSystemResource(java.lang.String)。  

在委托前,使用下面的算法从给定的资源名构造一个绝对资源名:

  • 如果 name'/' 开始 ('\u002f'),则绝对资源名是 '/' 后面的name 的一部分。
  • 否则,绝对名具有以下形式:
       modified_package_name/name
    

    其中 modified_package_name 是此对象的包名,该名用 '/' 取代了 '.' ('\u002e')。


 

==================================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址http://blog.csdn.net/ouyang_peng

==================================================================================================




阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
### 回答1: 此为在调试器的评估过程中发生的跳过断点错误,位于java.lang.class.forname0(java.lang.string, boolean, java.lang.classloader, java.lang.class)函数内。 ### 回答2: 首先,我们需要了解断点的概念。在使用调试器时,我们可以在程序中设置断点,以便在程序执行到该断点时暂停程序的执行,这可以使我们更容易地检查程序的状态和行为。 然而,在使用调试器时,有一种情况会出现“跳过断点”的情况,即当调试器遇到一个断点时,并不会暂停程序的执行。这种情况下,我们可以看到一个类似于这样的错误信息:“skipped breakpoint at java.lang.class.forname0(java.lang.string, boolean, java.lang.classloaderjava.lang.class) -1 because it happened inside debugger evaluation”。 这种情况的出现可能是由于我们在使用调试器时进行了一些调试器自己的计算或操作,而这些计算或操作可能会触发某些断点。此时,调试器会跳过这些断点,以使程序继续执行下去,而不会暂停程序的执行。 为了解决这个问题,我们可以尝试以下一些措施: 第一,我们可以尝试将调试器的结果计算放在别的地方,而不是直接在断点位置进行计算。这样可以避免调试器的计算触发了断点。 第二,我们可以尝试将断点设置在计算完成后的语句上。这样可以避免在断点位置进行计算,从而规避掉了问题。 总之,跳过断点的情况是一种常见的问题,在使用调试器时需要进行一定的防范。注意调试器的计算和操作,以避免出现这种情况。 ### 回答3: 这个错误信息是Java语言中常见的调试错误之一。通常来说,当我们在调试代码的时候,会在某个地方设置一个断点,程序在运行到这个断点时会暂停,然后我们可以通过查看变量、执行下一条语句等操作来进行调试。 但有时候,在代码执行到这个断点的时候,会出现一些问题,暂停的断点没有被触发,同时控制台会输出“skipped breakpoint”的警告。这可能会导致我们难以调试代码,影响我们的工作效率。 其中,这个具体的警告信息“skipped breakpoint at java.lang.class.forname0(java.lang.string, boolean, java.lang.classloaderjava.lang.class) -1 because it happened inside debugger evaluation”表示程序在执行这条语句时,已经进入了Dubugger执行状态,从而跳过了这个断点。而“java.lang.class.forname0”则是Java语言中负责加载类的函数,这个函数有些特殊,不能正常地进行断点调试。 如何解决这个问题呢?有两种方法可以尝试一下: 一是去掉或者移动这个断点。可以尝试在其他地方添加断点,或者暂时去掉这个断点,直到成功调试代码为止。 二是使用其他工具进行调试。可以使用一些其他的调试工具,比如Eclipse、IntelliJ IDEA等,这些工具对Java调试的支持更加完善,可以避免一些调试错误。 总之,对于Java代码调试过程中出现“skipped breakpoint”这个问题,需要我们耐心地排查,采取适当的措施,从而能够成功地调试代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

字节卷动

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值