有关getClassLoader().getResourceAsStream(fileName)、class.getResourceAsStream(fileName)和().getContextClassLoader().getResourceAsStream(fileName)的区别

一:前言

    在自己获取属性时,碰见了XX.class.getResourceAsStream(fileName),自己对这个其实不是很理解,上网查了下资料,又看到了上述的几个,所以就研究了下。

二:内容

  先说说着几个区别吧

  getClassLoader().getResourceAsStream("a.txt"):此方法的路径不管是绝对路径还是想对路径,都是从path下找得,即使"./a.txt"的形式去寻找,在此处我们要注意空指向异常的情况(可以看看这里写的http://cache.baiducontent.com/cm=9d78d513d99c12eb0fb1837e7c4380200e55f0326284915468d5e316ce370d160771e2cb30536713a0b66b6671f30e02b4e47132690c7af1dd8a9f4baea68f7871d57223706bdd124d9b58e5dc46529e778d1bb3f25cf0ba8768d5f18cc4de20089c44040c84f3895803&p=9274c54ad5c246e74bbe9b7c4605bb&newp=867cc54ad5c246bc4bbe9b7c4f0a9f231610db2151d6d11f6cc7&user=baidu&fm=sc&query=maven+getClassLoader%CE%AAnull&qid=bf473e2d0001b6a7&p1=3)。其中写到了Bootstrap ClassLoader (启动类加载器或者叫引导类加载器)加载jdk核心的APIs,这些APIs一般位于jdk_home/lib下;它是一个本地接口,所以不能从java代码中得到它的信息。例如, log(java.lang.String.class.getClassLoader())得到的是null(引用自http://blog.csdn.net/benjieming_wang/article/details/5623492)。classLoader是一种类加载器.

现在我就来附上代码说明吧:

package org.wh.properties;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

public class PropertiesDemo01 {
    Properties prop;
    public void setProp(Properties prop) {
        this.prop = prop;
    }
    public static void main(String args[]){
        loadConfig();
    }
    public static  Properties loadConfig(){
        Properties prop = new Properties();
        //Class的getResource()方法是从当前.class文件路径查找资源,而ClassLoader则是从jar包中查找
        //InputStream input = Properties.class.getResourceAsStream("/jdbcUtils.properties");
        InputStream input1 = PropertiesDemo01.class.getClassLoader().getResourceAsStream("jdbcUtils.properties");
        System.out.println("jdk的类加载器"+Properties.class.getClassLoader());//结果:jdk的类加载器:null
        System.out.println("demo:"+PropertiesDemo01.class.getClassLoader());//结果:demo:[email protected]
        try {
            prop.load(input1);
            @SuppressWarnings("rawtypes")
            Set keyValue=prop.keySet();
            for(Iterator a=keyValue.iterator();a.hasNext();){
                String key=(String) a.next();
                String value=prop.getProperty(key);
                System.out.println("-key的值--"+key+"value--的值-"+value);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return prop;
    }
}
结论:如果在这里用jdk的内部类就会报空指向异常,我们可以看到Properties.class.getClassLoader()得到的结果是null;

class.getResourceAsStream(fileName):fileName中加了“/”则代表了绝对路径,否则就是相对路径。代码可见上述再说说Thread.currentThread().getContextClassLoader():这个是当前线程的类加载器,
package org.wh.properties;

public class Test {

    public static void main(String[] args) {

        System.out.println(Thread.currentThread().getContextClassLoader());//当前线程的类加载器
        System.out.println(Test.class.getClassLoader());//当前类的类加载器
        System.out.println(ClassLoader.getSystemClassLoader());//系统初始的类加载器
    }

}
参考资料可以看看这里:http://stackoverflow.com/questions/676250/different-ways-of-loading-a-file-as-an-inputstream  
时间: 2024-08-04 16:40:03

有关getClassLoader().getResourceAsStream(fileName)、class.getResourceAsStream(fileName)和().getContextClassLoader().getResourceAsStream(fileName)的区别的相关文章

Class.forName() 初始化、Thread.currentThread().getContextClassLoader().getResourceAsStream

Class.forName() 和 ClassLoader.loadClass()的区别? Class.forName() 和 Class.forName().NewInstance()的区别? Class.forName("xx.xx")等同于Class.forName("xx.xx",true,CALLClass.class.getClassLoader()),第二个参数(bool)表示装载类的时候是否初始化该类,即调用类的静态块的语句及初始化静态成员变量. C

用Thread.currentThread().getContextClassLoader().getResourceAsStream读取配置文件

Java 用的路径分为相对路径和绝对路径: 具体又分为四种: 1.URI形式的绝对资源路径 如:file:/D:/java/eclipse/workspace/j/bin/a URI包括URL和URN两个类别,URL是URI的子集,所以URL一定是URI,而URI不一定是URL URL是URI的特例(一个标准的URL必须包括:protocol.host.port.path.parameter.anchor) URL的前缀/协议,必须是Java熟悉的.URL可以打开资源,而URI则不行.URL和U

导入properties时的坑

不用框架的方法导入properties文件时,除了把文件放在resource下面,网上查到的方法都找不到文件,一直报空指针异常. 自己设坑:一开始为了测试方便,把property放在了本地绝对路径下,使用下面代码能够导入. File f = new File("D:/x.properties"); Properties p = new Properties(); p.load(new FileInputStream(f)); 后要改相对路径,变为new File("x.pro

java.nio.file.InvalidPathException: Illegal char <:>

一.报错: java.nio.file.InvalidPathException: Illegal char <:> at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182) at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153) at sun.nio.fs.WindowsPathParser.parse(WindowsPathParse

关于class.getResourceAsStream(String name)与 class.getClassLoader().getResourceAsStream(String name)的路径name问题

关于class.getResourceAsStream(String name)与  class.getClassLoader().getResourceAsStream(String name)的路径name问题 首先用类加载资源文件的方式可以有以下三种: (包结构图) 1)   InputStream inStream = DaoFactory.class.getResourceAsStream("dao.properties"); 2)   inStream=DaoFactory

ClassLoader.getResourceAsStream(name); 获取配置文件的方法

ClassLoader.getResourceAsStream(name);路径问题 InputStream in = getClass().getResourceAsStream('/'+"spring-beans.dtd"); 表示从classs目录下面的找文件,文件放在src下面就可以了.InputStream in = getClass().getResourceAsStream("spring-beans.dtd"); 表示从当前classs下面的路径找文

对Class.getResourceAsStream和ClassLoader.getResourceAsStream方法所使用的资源路径的解释 (转)

对Class.getResourceAsStream和ClassLoader.getResourceAsStream方法所使用的资源路径的解释 标签: classgetResourceAsStreampath 2014-08-22 11:21 13675人阅读 评论(0) 收藏 举报 版权声明:本文为博主原创文章,未经博主允许不得转载. 这是个很基础的问题了,这里提供一些示例,帮助快速理解和记忆这个问题. 在该方法的文档:http://docs.oracle.com/javase/7/docs/

ClassLoader.getResourceAsStream() 与 Class.getResourceAsStream()的区别

package com.xinwei.util; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class PropertiesLoader { public static void main(String[] args) { // test1(); test2(); } //在使用Class.getResourceAsStream 时, 资源路径有两种方式,

织梦多语言站点,{dede:include filename=&#39;&#39;/}引入问题

织梦模板include插入非模板目录文件出现"无法在这个位置找到"错误的解决办法 以下是dede V55_UTF8 查dede include标签手册 (3) include 引入一个文件,形式为:{dede:include file='文件名称' ismake='是否为dede板块模板(yes/no)'/}对文件的搜索路径为顺序为:绝对路径.include文件夹,CMS安装目录,CMS主模板目录 其实根本不是这个样子的,如果你要引用一个其它目录的东西如:{dede:include f