介绍两种获取资源的方式:
一、通过ClassLoader获取:
loader.getResourceAsStream("a/b/temp.txt");--获取src下/a/b包下的temp.txt资源
二、通过Class获取:
加"/":
与ClassLoader一样。class.getResourceAsStream("/a/b/temp.txt")
不加"/":
class.getResourceAsStream("temp.txt")。表示获取当前类路径下的资源。
举个栗子:
【项目路径如下】
![3f9c793ccbd4e07743425636d8e52814.png](https://img-blog.csdnimg.cn/img_convert/3f9c793ccbd4e07743425636d8e52814.png)
【classes路径如下】
![bbc54a6b6c07325da524582510432bb6.png](https://img-blog.csdnimg.cn/img_convert/bbc54a6b6c07325da524582510432bb6.png)
【测试代码】
package com.io.test;import java.io.InputStream;public class TestIo { public static void main(String[] args) {