码迷,mamicode.com
首页 > 其他好文 > 详细

读取properties文件

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

标签:class   import   param   ret   获取   fileinput   return   oid   nbsp   

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文件

标签:class   import   param   ret   获取   fileinput   return   oid   nbsp   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!