java中读取properties的工具类
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Calendar; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Properties; @SuppressWarnings("unchecked") public class GlobalConfig { public static final String SYSTEM_PROPERTIES = "/system.properties"; private static Map propertieMap = new HashMap(); private static Map propertieFileMap = new HashMap(); static { Properties properties = init("/system.properties"); /** * properties文件的内容如下:在maven管理中放在了src/main/resources/local目录下面 * 这样可以把所有的properties文件全部加载到内存中,且加载一次 * * system.properties的内容如下: * sys.core=systemCore.properties * y_system=y_system.properties * sys.boss=bossInterface.properties * * bossInterface.properties 的内容如下: * sBookSelfInfoUrl=http://10.204.*.*:51*** /esbWS/services/sBookSelfInfo * sBookTermInfoUrl=http://10.204.*.*:51*** /esbWS/services/sBookTermInfo */ Iterator it = properties.keySet().iterator(); /** * it 中保存的相当于sys.core和y_system和sys.boss */ while (it.hasNext()) { //name的值依次是sys.core和y_system和sys.boss String name = (String) it.next(); //file的值依次是systemCore.properties,y_system.properties,bossInterface.properties String file = properties.getProperty(name); file = file.trim(); propertieFileMap.put(name, file); Properties p = init("/" + file); //把内存中的 properties文件保存到map中 propertieMap.put(name, p); } } /** * 根据传过来的properties文件的路径,把该文件 加载到内存中 * @param propertyFile * @return */ private static Properties init(String propertyFile) { Properties p = new Properties(); try { p.load(GlobalConfig.class.getResourceAsStream(propertyFile)); } catch (Exception e) { e.printStackTrace(); } return p; } /** * * @param cls 根据该参数值找到保存到map中的properties文件 * @param name 根据该参数name在找到的properties文件中读取 相应的值 * @return * * * java代码使用的例子 String url = GlobalConfig.getProperty("sys.boss", "s1166Query"); */ public static String getProperty(String cls, String name) { Properties p = (Properties) propertieMap.get(cls); if (p != null) { return p.getProperty(name); } return null; } public static boolean getBooleanProperty(String cls, String name) { String p = getProperty(cls, name); return "true".equals(p); } public static Integer getIntegerProperty(String cls, String name) { String p = getProperty(cls, name); if (p == null) { return null; } return Integer.valueOf(p); } public static Long getLongProperty(String cls, String name) { String p = getProperty(cls, name); if (p == null) { return null; } return Long.valueOf(p); } public static Double getDoubleProperty(String cls, String name) { String p = getProperty(cls, name); if (p == null) { return null; } return Double.valueOf(p); } public static void store() { } public static void store(String cls) { Properties p = (Properties) propertieMap.get(cls); try { FileOutputStream fi = new FileOutputStream(new File( (String) propertieFileMap.get(cls))); p.store(fi, "Modified time: " + Calendar.getInstance().getTime()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }