java获取properties配置文件值

package me.ilt.Blog.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertiesUtil {
	public static String getValue(String key){
		Properties prop = new Properties();
		try {
			//装载配置文件
			prop.load(new FileInputStream(new File("src//dbCfg.properties")));
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		//返回获取的值
		return prop.getProperty(key);
	}
	/*public static void main(String[] args) {
		System.out.println(getValue("dbUser"));
	}*/
}
 

方法二

package me.ilt.Util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesUtil {
	public static String getValue(String key){
		Properties prop = new Properties();
		InputStream in = new PropertiesUtil().getClass().getResourceAsStream("/dbCfg.properties");
		try {
			prop.load(in);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return prop.getProperty(key);
	}

}
posted @ 2017-10-10 23:43  人情世故  阅读(1872)  评论(0编辑  收藏  举报
"不管做什么,只要坚持下去就会看到不一样!在路上,不卑不亢!"