首页 > 其他 > 详细

读取properties文件

时间:2017-08-23 15:50:10      阅读:311      评论:0      收藏:0      [点我收藏+]

package com.hm.common.util;

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

public class PropertiesUtil {
final static String fileName = "/conf/common.properties";

// 获取key所对应的值
public static String getProperties(String key) {
Properties prop = new Properties();
InputStream is = null;
try {
// properties文件放在src目录的下边
is = PropertiesUtil.class.getResourceAsStream(fileName);
if (is == null)
is = new FileInputStream(fileName);
prop.load(is);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return prop.getProperty(key);
}

public static void main(String[] args) {
System.out.println( PropertiesUtil.getProperties("a.vn.salt"));
}

}

 

package com.la.util;

import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.logging.Logger;

public class configResponse {

// protected static Logger logger = Logger.getLogger(configResponse.class);
protected static ResourceBundle configResponse;

protected static final String config_response_file="config/configResponse.properties";

static {

try {
configResponse = PropertyResourceBundle.getBundle(config_response_file);

} catch (Exception e) {
// TODO: handle exception
}
}

public static int getIntegerCode(String key) {
try {
if (configResponse.getString(key) != null) {
return Integer.parseInt(configResponse.getString(key));
}
} catch (Exception e) {
// logger.error("RESPONSE CODE FROM STRING TO INT THEN ERROR." , e) ;
}
return 0 ;
}
/**
* update by Brain
* @param key
* @return
*/
public static String getStringCode(String key){
return configResponse.getString(key)!=null?configResponse.getString(key):"";
}

public static int getIntegerCode(String key , int value ) {
try {
if (configResponse.getString(key) != null) {
return Integer.parseInt(configResponse.getString(key));
}
} catch (Exception e) {
// logger.error("RESPONSE CODE FROM STRING TO INT THEN ERROR." , e) ;
}
return value ;
}

public static String get(String key) {
return configResponse.getString(key);
}

public static String get(String key , String value) {
return configResponse.getString(key)==null?value:configResponse.getString(key) ;
}


}

读取properties文件

原文:http://www.cnblogs.com/lovelanglangyou/p/7418531.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!