Java如何读取Properties文件中的属性并获取参数值呢?
下文笔者讲述读取Properties文件中的属性的方法分享,如下所示
实现思路: 采用Properties.class.getClassLoader() .getResourceAsStream 方法即可读取Properties文件例:
package com.java265.util; import java.io.InputStream; import java.util.Properties; public class PropertyUtil { private static Properties p = null; public synchronized static void initP(String propertyName) throws Exception { if (p == null) { p = new Properties(); InputStream inputstream = Properties.class.getClassLoader() .getResourceAsStream(propertyName);//<span style="color: rgb(0, 0, 255); font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 14px; line-height: 25px;">abc.properties</span> if (inputstream == null) { throw new Exception("inputstream " + propertyName + " open null"); } p.load(inputstream); inputstream.close(); inputstream = null; } } public static String getValueByKey(String propertyName, String key) { String result = ""; try { initP(propertyName); result = p.getProperty(key); return result; } catch (Exception e) { e.printStackTrace(); return ""; } } public static void main(String[] s) { // System.out.println(PWSProperties.getValueByKey("pws.properties","ws_split_chars")); } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。