java 资源文件加载路径

来源:互联网 发布:淘宝摩托车专卖店 编辑:程序博客网 时间:2024/05/17 06:49
import java.io.InputStream;import java.io.InputStreamReader;import java.util.Properties;/** * 读取配置文件的工具类 */public class ReadConfigFile {public static void main(String args[]) {// 1.默认加载 src 根目录下的文件,如果没有将会报错ReadConfigFile configConstant = new ReadConfigFile("constant.properties");// 2.加载根目录下指定目录的资源文件,这个加载的就是 bin/dbconfig 目录下的 db.properties 文件ReadConfigFile configDb = new ReadConfigFile("./dbconfig/db.properties");System.out.println(configConstant.getValue("name"));System.out.println(configDb.getValue("ip"));}private Properties properties = null;private InputStream in = null;public ReadConfigFile(String fileConfig) {try {in = this.getClass().getClassLoader().getResourceAsStream(fileConfig);properties = new Properties();properties.load(new InputStreamReader(in, "utf-8"));in.close();} catch (Exception e) {e.printStackTrace();}}public String getValue(String key) {String value = null;try {value = properties.getProperty(key);} catch (Exception e) {}return value;}}

0 0
原创粉丝点击