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-03-14 15:02  望星辰大海  阅读(16728)  评论(0编辑  收藏  举报