调用 getResourceAsStream 获取类路径下的文件对应的输入流.

public class ReflectionTest {
    @Test
    public void testClassLoader() throws FileNotFoundException{
        //src目录下,直接加载
        InputStream in1 = null;
        in1 = this.getClass().getClassLoader().getResourceAsStream("test1.txt");
        
        //放在内部文件夹,要写全路径
        InputStream in2 = null;
        in2 = this.getClass().getClassLoader().getResourceAsStream("com/atguigu/java/fanshe/test2.txt");
    }
}

猜你喜欢

转载自blog.csdn.net/baidu_20608025/article/details/85326270
今日推荐