加載配置經常使用java
import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import java.util.Properties; import org.apache.log4j.Logger; /** * 描述:讀取配置文件工具類<br> * 做者:<br> * 修改日期:2014年11月10日上午12:01:03 <br> * E-mail: yqdsz0464@sinosoft.com.cn <br> */ public class PropertiesUtil { private static final Logger logger = Logger.getLogger(PropertiesUtil.class); private static final Map<String, String> map= new HashMap<String, String>(); private static Properties properties = new Properties(); /** * 方法名稱: init<br> * 描述:加載配置文件 * 做者: * 修改日期:2014年11月15日下午5:58:10 * @param filepath * @throws IOException */ public static void init(String filepath) throws IOException{ if(map.containsKey(filepath)){ } else{ InputStreamReader inputStream = null; try { inputStream = new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(filepath),"UTF-8"); properties.load(inputStream); } finally{ if(inputStream != null){ try { inputStream.close(); } catch (IOException e) { logger.warn(filepath+"資源關閉失敗"); } } } map.put(filepath, filepath); } } /** * 方法名稱: getValue<br> * 描述:根據key獲取配置文件中的value * 做者: * 修改日期:2014年11月15日下午5:58:23 * @param key * @return */ public static String getValue(String key){ String value = ""; value = properties.getProperty(key); value = null == value || "".equals(value)? "" : value.trim(); return value; } }