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);
	}

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM