工具类
实现配置文件的读取
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Properties;
public class ReadConfigUtil {
//配置文件路径
private String configPath;
//配置文件对象化Properties
private Properties property;
//构造方法加载配置文件
public ReadConfigUtil(String configPath) throws Exception {
this.configPath = configPath;
//方法一:加载字节流
//InputStream is = ReadConfigUtil.class.getClassLoader().getResourceAsStream(this.configPath);
//方法二:加载字节流
InputStream is = ClassLoader.getSystemResourceAsStream(this.configPath);
转换为字符
Reader reader = new InputStreamReader(is,"utf-8");
property = new Properties();
property.load(reader);
reader.close();
}
//配置文件内值得拿取
public String getValue(String key){
return property.getProperty(key);
}
//工具类的测试
public static void main(String[] args) throws Exception {
String conf="application.properties";
ReadConfigUtil readConfigUtil = new ReadConfigUtil(conf);
System.out.println(readConfigUtil.getValue("bdb_index_root_dir"));
}
}