java读取资源文件代码

xiaoxiao2022-06-12  32

package com.huawei.pms.realtime.util; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.apache.log4j.Logger; /** * * 用于处理 properties 文件,从中获得配置信息 * * PropertiesOperator * @author fhuan123 * 2009-7-6上午09:03:50 * @phone 13733836515 */ public class PropertiesOperator { private static Logger logger = Logger.getLogger(PropertiesOperator.class .getName()); /** * @describe 从固定的配置文件中,根据指定的 key 来查找对应的属性 * * @param key 对应的键 * */ public static String getValueByKey(String key) { String result = null; Properties p = new Properties(); InputStream in = PropertiesOperator.class.getClassLoader() .getResourceAsStream("config.properties"); try { p.load(in); result = p.getProperty(key); } catch (IOException e) { logger.debug("check host --- load properties happend err"); e.printStackTrace(); } finally { try { in.close(); } catch (Exception e) { logger.error(e) ; } } return result; } /** * @describe 根据指定的配置文件名和Key 返回 key 的属性值 * @param name 配置文件名 * @param key 键 * @return 返回 属性值 */ public static String getValueByKey(String name, String key) { String result = null; Properties p = new Properties(); InputStream in = PropertiesOperator.class.getClassLoader() .getResourceAsStream(name); try { p.load(in); result = p.getProperty(key); } catch (IOException e) { logger.debug("check host --- load properties happend err"); e.printStackTrace(); } finally { try { in.close(); } catch (Exception e) { logger.error(e) ; } } return result; } public static void main(String[] args) { // System.out.println(getValueByKey("INFO_FILE_PATH")) ; // System.out.println(getValueByKey("masterIp")) ; // System.out.println(getValueByKey("masterPort")) ; // System.out.println(getValueByKey("configservice.properties","hostName")) ; } }

 

相关资源:Java 读取资源文件
转载请注明原文地址: https://www.6miu.com/read-4933239.html

最新回复(0)