首页 > 代码库 > Class.getResourceAsStream()和ClassLoader.getResourceAsStream()

Class.getResourceAsStream()和ClassLoader.getResourceAsStream()

Edit

 

Class.getResourceAsStream()和ClassLoader.getResourceAsStream()


注意三点:

  • 1.用Class.getResourceAsStream() 时,路径应该是以"/"开头的,如:
    mypackage.Hello.class.getResourceAsStream("/config/config.ini");

  • 2.如果直接用ClassLoader.getResourceAsStream()不用以"/"开头.如,mypackage.Hello.class.getResourceAsStream("config/config.ini");

  • 3.注意文件放置的位置,应该放在classpath下.如 : 工程文件中的src下.


Class.getResourceAsStream()ClassLoader.getResourceAsStream()
这两个方法的区别是资源的定义不同, 一个主要用于相对与一个该Class类取资源,而另一个用于取相对于classpath的资源,用的是绝对路径。

1.在使用Class.getResourceAsStream时, 资源路径有两种方式, 一种以 /开头,则这样的路径是指定绝对路径, 如果不以/开头, 则路径是相对与这个class所在的包的。

2.在使用ClassLoader.getResourceAsStream时, 路径直接使用相对于classpath的绝对路径。
举例,下面的三个语句,实际结果是一样的:

com.explorers.Test.class.getResourceAsStream("abc.jpg")
= com.explorers.Test.class.getResourceAsStream("/com/explorers/abc.jpg")
= ClassLoader.getResourceAsStream("com/explorers/abc.jpg")

@%2802.JavaSE%29%5BClass%2CClassLoader%2CgetResource%2CgetResourceAsStream%5D%0A%23%20Class.getResourceAsStream%28%29%u548CClassLoader.getResourceAsStream%28%29%0A%0A-----%0A**%u6CE8%u610F%u4E09%u70B9%3A**%0A-%201.%u7528%60Class.getResourceAsStream%28%29%60%20%u65F6%2C%u8DEF%u5F84%u5E94%u8BE5%u662F%u4EE5%60%22/%22%60%u5F00%u5934%u7684%2C%u5982%3A%0A%60mypackage.Hello.class.getResourceAsStream%28%22/config/config.ini%22%29%3B%60%0A%0A-%202.%u5982%u679C%u76F4%u63A5%u7528%60ClassLoader.getResourceAsStream%28%29%20%60%u4E0D%u7528%u4EE5%60%22/%22%60%u5F00%u5934.%u5982%2C%60mypackage.Hello.class.getResourceAsStream%28%22config/config.ini%22%29%3B%60%0A%0A-%203.%u6CE8%u610F%u6587%u4EF6%u653E%u7F6E%u7684%u4F4D%u7F6E%2C%u5E94%u8BE5%u653E%u5728%60classpath%60%u4E0B.%u5982%20%3A%20%u5DE5%u7A0B%u6587%u4EF6%u4E2D%u7684src%u4E0B.%0A%0A------%0A**%60Class.getResourceAsStream%28%29%60%u548C%60ClassLoader.getResourceAsStream%28%29%60**%0A%u8FD9%u4E24%u4E2A%u65B9%u6CD5%u7684%u533A%u522B%u662F%u8D44%u6E90%u7684%u5B9A%u4E49%u4E0D%u540C%uFF0C%20%u4E00%u4E2A%u4E3B%u8981%u7528%u4E8E%u76F8%u5BF9%u4E0E%u4E00%u4E2A%u8BE5Class%u7C7B%u53D6%u8D44%u6E90%uFF0C%u800C%u53E6%u4E00%u4E2A%u7528%u4E8E%u53D6%u76F8%u5BF9%u4E8Eclasspath%u7684%u8D44%u6E90%uFF0C%u7528%u7684%u662F%u7EDD%u5BF9%u8DEF%u5F84%u3002%0A%0A1.%u5728%u4F7F%u7528%60Class.getResourceAsStream%20%60%u65F6%uFF0C%20%u8D44%u6E90%u8DEF%u5F84%u6709%u4E24%u79CD%u65B9%u5F0F%uFF0C%20%u4E00%u79CD%u4EE5%20%60/%20%60%u5F00%u5934%uFF0C%u5219%u8FD9%u6837%u7684%u8DEF%u5F84%u662F%u6307%u5B9A%u7EDD%u5BF9%u8DEF%u5F84%uFF0C%20%u5982%u679C%u4E0D%u4EE5%60%20/%20%60%u5F00%u5934%uFF0C%20%u5219%u8DEF%u5F84%u662F%u76F8%u5BF9%u4E0E%u8FD9%u4E2Aclass%u6240%u5728%u7684%u5305%u7684%u3002%0A%0A2.%u5728%u4F7F%u7528%60ClassLoader.getResourceAsStream%60%u65F6%uFF0C%20%u8DEF%u5F84%u76F4%u63A5%u4F7F%u7528%u76F8%u5BF9%u4E8Eclasspath%u7684%u7EDD%u5BF9%u8DEF%u5F84%u3002%0A%u4E3E%u4F8B%uFF0C%u4E0B%u9762%u7684%u4E09%u4E2A%u8BED%u53E5%uFF0C%u5B9E%u9645%u7ED3%u679C%u662F%u4E00%u6837%u7684%uFF1A%0A%0A%60com.explorers.Test.class.getResourceAsStream%28%22abc.jpg%22%29%60%0A%3D%20%60com.explorers.Test.class.getResourceAsStream%28%22/com/explorers/abc.jpg%22%29%60%0A%3D%20%60ClassLoader.getResourceAsStream%28%22com/explorers/abc.jpg%22%29%60

 

Class.getResourceAsStream()和ClassLoader.getResourceAsStream()