给一个路径,看到有时会用getClass().getClassLoader().getResourceAsStream()来得到一个InputStream对象
请跟直接new一个出来有什么不一样,比如说都给一个绝对名字:fileName = D:\test\fuck.txt,那么:
InputStream in = getClass().getClassLoader().getResourceAsStream(fileName);

InputStream in = new FileInputStream(fileName);
有啥不同?

解决方案 »

  1.   

    一个是基于文件系统的 InputStream一个是基于类加器的 InputStream完全不一样。
      

  2.   


    +1表现为寻址方式不一样假如当前类T1的应用中的包路径是 t1.T1,同时在类T1下面还有一个123.txt的文件
    文件系统中以类似[C:\\project\\project1\\t1\\123.txt]的路径方式访问。
    类加载器可以用
    getClass().getClassLoader().getResourceAsStream("t1/123.txt");
      

  3.   

    给个详细例子看看,之前没接触这些~~~
    现在也是用getResourceAsStream来加载配置文件,也是突然想到这两种都可以得到一个InputStream
    想看看在具体的表现上有啥不同,谢谢