ClassLoad类加载器读取ClassPath路径下的配置文件(一)
一、项目介绍
1、使用 com.test.test包下的ReadclassLoad 类去读取另一个包 com.test.util 包 ClassPath路径下的properties 文件。项目路径如下图:
2、实例代码
package com.test.test;
import java.io.IOException;
import java.io.InputStream;
import org.junit.Test;
public class ReadclassLoad {
//通过类加载器获取 ClassPath 路径下的文件
@Test
public void readLoad() throws IOException{
//1、获取类加载器
ClassLoader cd = ReadclassLoad.class.getClassLoader();
//2、用类加载器读取文件信息
InputStream in = cd.getResourceAsStream("jdbc.properties");
//3、创建数组,遍历文件信息
byte[] by = new byte[in.available()];
int len = in.read(by);
System.out.println(new String(by,0,len));
}
}
3、运行结果,读取到配置文件的配置信息