java 如何读取配置文件

来源:互联网 发布:excel取消数据验证 编辑:程序博客网 时间:2024/06/04 19:55
很多时候,经常改动的变量都会写入配置文件里面,程序里面再读入。


common.properties
OERP_TABLESPACE=APPS.






工具类CommonUtils.java
import java.util.Properties;public class CommonUtils {//根据文件名获取属性文件public static Properties getProperties(String propFileName) throws IOException{InputStream in = CommonUtils.class.getClassLoader().getResourceAsStream(propFileName+".properties");Properties prop = new Properties();  prop.load(in);return prop;}//根据属性名获取属性值public static String getProperty(String propFileName,String propName) throws IOException{Properties prop = getProperties(propFileName);return prop.getProperty(propName);}}




cotroller调用:

String savePath="OERP_TABLESPACE"; String val = CommonUtils.getProperty("common", savePath);//获得配置文件的值 



原创粉丝点击